You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unsafe type mismatch of destructuring assignment #6125
A following valid destructuring assignment always occurs a TypeError.
// typescriptvar{x: {y}}: {x?: {y?}}={};// should be a compile error// emitvary={}.x.y;// TypeError: (intermediate value).x is undefined// javascript on firefoxvar{x:{y}}={};// TypeError: can't convert undefined to object
Destructuring assignment should constrain type declarations.
Destructuring assignments violate a following type constraint.
varoptions: {x?: {y?}};varrequired: {x: {y}}=options;// compile error, but destructuring assignments are not.var{x: {y}}: {x?: {y?}}={};// should be a compile error
Destructuring assignments have a own implicit type but TypeScript breaks it type improperly.
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.
vara: {x?: {y?}}={};// ok since x and y are optionalvar{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
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.
varo: {x?: {y?: any}};y=o.x.y;
But it does not have my suggested problem.
varo: {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.
// typescriptvar{x: {y}}: {x?: {y?}}={};// should be a compile error// emitvary={}.x.y;// TypeError: (intermediate value).x is undefined
It error can escape manually in your code:
varo: {x?: {y?: any}};if(o.x){y=o.x.y;}
Destructuring assignment is not:
vary={}.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:
// use destructuring assignmentvar{x: {y}}: {x?: {y?}}={};// runtime error, explicit type declaration erase the type checking and safety
// use functionvary;f({});// compile error, before the runtime errorfunctionf(o: {x: {y: any}}){y=o.x.y;}
f function does not accept the implicit type cast of own parameter type because f function does not work if anyone cast own parameter type. But destructuring assignment accept the implicit(not explicit) type cast of own type for the acceptance of right-hand type, and it occurs an error. An explicit type declaration of destructuring assignment erase the type checking and safety. This is nonsense.
Activity
vladima commentedon Dec 18, 2015
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.
if optionality is removed then compiler will error since some fields on the left hand side are optional when they should be required
falsandtru commentedon Dec 18, 2015
I think, implicit type of
({x: {y}} = o)should be:{x: {y}} = {}, not:{x?: {y?}} = {}.DanielRosenwasser commentedon Dec 18, 2015
Why would that be an error, but not the following?
falsandtru commentedon Dec 18, 2015
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.
But it does not have my suggested problem.
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.
It error can escape manually in your code:
Destructuring assignment is not:
Therefore destructuring assignment type must be a not optional type:
however, accept an optional type if destructuring assignment has default value:
or should generate a executable valid code:
Points
Destructuring assignment type is not optional if TypeScript generate an invalid code:
because destructuring assignment requires the accessibility of target element:
An optional type cannot assign to a not optional type:
Therefore destructuring assignment cannot declare the explicit optional type:
also:
however:
Or, generate a valid code:
What do you think about a requirement of accessibility in destructuring assignment?
falsandtru commentedon Dec 18, 2015
These are a same processing:
ffunction does not accept the implicit type cast of own parameter type becauseffunction does not work if anyone cast own parameter type. But destructuring assignment accept the implicit(not explicit) type cast of own type for the acceptance of right-hand type, and it occurs an error. An explicit type declaration of destructuring assignment erase the type checking and safety. This is nonsense.