-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unsafe type mismatch of destructuring assignment #6125
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
I'm not sure that I'm following you here. In all cases that you claim as illegal you've explicitly stated that properties are optional so compiler does not try to enforce their presence. I.e. var a: {x?: {y?}} = {}; // ok since x and y are optional
var {x: {y}}: {x?: {y?}} = a; // ok - types match if optionality is removed then compiler will error since some fields on the left hand side are optional when they should be required |
I think, implicit type of var o: {x?:{y?}}, y;
({x: {y}} = o); // valid, but should be an compile error |
Why would that be an error, but not the following? var o: {
x?: {
y?: any
}
};
y = o.x.y; |
Because type of destructuring assignment should not be an optional. Your code example will be an error but right because it accepted by an explicit type. var o: {
x?: {
y?: any
}
};
y = o.x.y; But it does not have my suggested problem. var o: {x?:{y?}}, y;
({x: {y}} = o); // valid, but should be an compile error If destructuring assignment has a implicit type, it will be a not optional type because a following code always throw TypeError. Destructuring assignment requires the accessibility of target element. // typescript
var {x: {y}}: {x?: {y?}} = {}; // should be a compile error
// emit
var y = {}.x.y; // TypeError: (intermediate value).x is undefined It error can escape manually in your code: var o: {
x?: {
y?: any
}
};
if (o.x) {
y = o.x.y;
} Destructuring assignment is not: var y = {}.x.y; // cannot fix this generated incorrect code Therefore destructuring assignment type must be a not optional type: var {x: {y}}: {x: {y}} = ...; however, accept an optional type if destructuring assignment has default value: var o: {x?: {y?}}, y;
({x: {y = 0} = {y: 0}} = o); // ok or should generate a executable valid code: // typescript
var {x: {y}}: {x?: {y?}} = {};
// emit
var tmp = {};
var y = tmp.x ? tmp.x.y : void 0; Points
What do you think about a requirement of accessibility in destructuring assignment? |
These are a same processing: // use destructuring assignment
var {x: {y}}: {x?: {y?}} = {}; // runtime error, explicit type declaration erase the type checking and safety // use function
var y;
f({}); // compile error, before the runtime error
function f(o: {x: {y: any}}) {
y = o.x.y;
}
|
A following valid destructuring assignment always occurs a TypeError.
Destructuring assignment should constrain type declarations.
Destructuring assignments violate a following type constraint.
Destructuring assignments have a own implicit type but TypeScript breaks it type improperly.
The text was updated successfully, but these errors were encountered: