mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-07 05:41:22 -06:00
Binding element with parent type any is any
Previously if the binding element had an initializer, then that type
would be used. But this is incorrect:
```ts
function f(x: any) {
let { d = 1 } = x;
// d should have type any not number.
// f can be called with anything:
}
f({ d: 0 });
f({ d: 'hi' });
f({});
```
This commit is contained in:
parent
43084829bc
commit
da41217f43
@ -4151,13 +4151,13 @@ namespace ts {
|
||||
if (parentType === unknownType) {
|
||||
return unknownType;
|
||||
}
|
||||
// If no type was specified or inferred for parent, or if the specified or inferred type is any,
|
||||
// infer from the initializer of the binding element if one is present. Otherwise, go with the
|
||||
// undefined or any type of the parent.
|
||||
if (!parentType || isTypeAny(parentType)) {
|
||||
if (declaration.initializer) {
|
||||
return checkDeclarationInitializer(declaration);
|
||||
}
|
||||
// If no type was specified or inferred for parent,
|
||||
// infer from the initializer of the binding element if one is present.
|
||||
// Otherwise, go with the undefined type of the parent.
|
||||
if (!parentType) {
|
||||
return declaration.initializer ? checkDeclarationInitializer(declaration) : parentType;
|
||||
}
|
||||
if (isTypeAny(parentType)) {
|
||||
return parentType;
|
||||
}
|
||||
|
||||
@ -4183,9 +4183,6 @@ namespace ts {
|
||||
// computed properties with non-literal names are treated as 'any'
|
||||
return anyType;
|
||||
}
|
||||
if (declaration.initializer) {
|
||||
getContextualType(declaration.initializer);
|
||||
}
|
||||
|
||||
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
|
||||
// or otherwise the type of the string index signature.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user