Search Terms
array destructuring, array destructuring inference
Suggestion
When you use array destructuring the type is inferred incorrectly. I hope something can be changed to enforce proper type inference when using this. In my code, TS has helped clean and prevent a lot of mistakes. The only mistakes that I repeatedly find being made either by me or others in my project are always related to this. Time and time again I have had these errors happen in runtime making the benefits of Typescript lost. An option to enforce this would be amazing, please. (Especially if it is just included inside the strict option.)
Use Cases
constexample=(args: string[])=>{const[userID,duration, ...reason]=args// userID and duration is AUTOMATICALLy inferred to be a string here. // However, if for whatever reason args is an empty array userID is actually `undefined` and NOT a `string`. // This is valid but it should not be because userID could be undefineduserID.toUpperCase()}Examples
constexample=(args: string[])=>{const[userID]=args// userID is AUTOMATICALLy inferred to be a string here. // However, if for whatever reason args is an empty array userID is actually `undefined` and NOT a `string`. // Typescript SHOULD warn that this is not possible on undefineduserID.toUpperCase()}Proper Way:
constexample=(args: string[])=>{const[userID,duration, ...reason]=argsif(!userID)return// This is valid but it should not be because userID could be undefineduserID.toUpperCase()}Checklist
My suggestion meets these guidelines:
Search Terms
array destructuring, array destructuring inference
Suggestion
When you use array destructuring the type is inferred incorrectly. I hope something can be changed to enforce proper type inference when using this. In my code, TS has helped clean and prevent a lot of mistakes. The only mistakes that I repeatedly find being made either by me or others in my project are always related to this. Time and time again I have had these errors happen in runtime making the benefits of Typescript lost. An option to enforce this would be amazing, please. (Especially if it is just included inside the
strictoption.)Use Cases
Examples
Proper Way:
Checklist
My suggestion meets these guidelines: