Repository: TypeScript
Issue: Strict `Array.join`
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

`array join type`

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).

Not 100% sure on the last one. I feel it would though.
The first, only if it's built as an opt-in option.

## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->

An option to force strict `Array.join` calls, where it can only join array elements that are a string, or are assignable to a string. The `join` function is allowed on any (including `any` 😉) type of array, even types that are completely incompatible with `string`. The `join` function basically leaves it as a black box that any kind of array type is magically transmogrified into a string... Somehow. That doesn't feel right to me.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

Here's a [Playground link](https://www.typescriptlang.org/play?#code/MYewdgzgLgBGCuBbARgUwE4RgXhgbQEYAaAJiIGYiAWIgVgF0BuAKGYHo2YAVACwEssAmAApQ6dKmBQANgE8AlHBCwAhtOkgA7qgAmzUJFhhlAKxB8wu6OgsBzAFwxrdnHCRpMhJqw7d+grDUNbR0iGGR4WAgeEHhpHTAAclg0fXBoGDMLKygbMAcnXJdcBBQMCAA6LLBhRMT5FiA) that illustrates the problem where this feature could protect against.

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->

When I'm doing a valid `Array.join` all is fine, but when I afterwards decide to change the type of the elements in that array and forget to do something about that `join`, it'll result in completely correct and transpilable code which produces weird behaviour at runtime. For example, if the array elements where a string, and now they have become some kind of object.

Checking if the element type of an array could be assigned to `string`, could protect against such mistakes. And to keep existing codebases compatible, I would suggest this be opt-in, in the `compilerOptions`. So much like there's a `strictFunctionTypes`, there could be an additional `strictArrayJoins` or similar.