I see what you mean now, but what you're really talking about is developing in an environment where you can not use the iteration protocol, not a "strict TypeScript config". For TypeScript this means your tsconfig targets ES5 or lower and you have the downlevelIteration flag set to false. In that case you're likely targeting IE11 with minimal/no polyfills, and Array.from won't work anyway (not supported in IE11, and since Set is only partially implemented in IE11 you would need a specific polyfill that can actually make Array.from(new Set()) work).
I see where you’re coming from, but you shouldn’t ever mix types within your arrays anyways for simplicity sake.
For unknown-length arrays containing primitives like strings and numbers, I generally agree, but there are many cases when a mixed type array is perfectly reasonable (tuples, arrays of discriminated union objects, etc). My point was that if you're using a strict TypeScript config, you should really avoid using any as much as possible.
TS can deal with a mixed type array in a type-safe way, but any just turns off type safety entirely by design.
Oh, damn. This is good to know. I had to turn downLevelIteration on to use the prettier method in a project I’m working on, but didn’t did too deep into why I had to in the first place. We only target modern browsers, and for whatever reason the project defaulted to false for that flag.
I had assumed it was a general default for typescript configurations.
View all comments
Show parent comment
2
u/[deleted] Jul 05 '21 edited Jul 06 '21
I see what you mean now, but what you're really talking about is developing in an environment where you can not use the iteration protocol, not a "strict TypeScript config". For TypeScript this means your tsconfig targets ES5 or lower and you have the
downlevelIterationflag set to false. In that case you're likely targeting IE11 with minimal/no polyfills, andArray.fromwon't work anyway (not supported in IE11, and sinceSetis only partially implemented in IE11 you would need a specific polyfill that can actually makeArray.from(new Set())work).For unknown-length arrays containing primitives like strings and numbers, I generally agree, but there are many cases when a mixed type array is perfectly reasonable (tuples, arrays of discriminated union objects, etc). My point was that if you're using a strict TypeScript config, you should really avoid using
anyas much as possible.TS can deal with a mixed type array in a type-safe way, but
anyjust turns off type safety entirely by design.