-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Destructing nested arrays/tuples does not infer type #38092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In the playground link you provided, typescript is already giving you exactly the right error message at exactly the right place that tells you exactly why this won't work.
Relevant discussion: #3500 (comment) Edit: Looks like you've edited your code so this response isn't relevant anymore. |
Yeah apologies, I have updated the code to demonstrate the problem more clearly. |
I can fix the issue (for now) by explicitly defining the type of the tuple, with: for (const [fieldName, emptyValue, expectedError] of [
['email', '', 'must enter an email address'],
['password', '', 'must enter a password'],
['tos', false, 'must accept the ToS'],
] as [string, any, string][]) {
fieldName.charAt(0);
} |
Without the type assertion you don't have a tuple, you just have an array. You can also use Changing this behavior (infer tuple instead of array by default) would be a major breaking change. |
OK thanks, makes sense. |
TypeScript Version: Nightly
Search Terms:
"typescript array destructing types not working"
Expected behavior:
fieldName
to be of typestring
emptyValue
to be of typeany
expectedError
to be of typestring
Actual behavior:
fieldName
is of typestring | boolean
emptyValue
is of typestring | boolean
expectedError
is of typestring | boolean
Related Issues:
#32465
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: