Repository navigation
Binding element with parent type any is of type any - #18955
Nathan Shively-Sanders (sandersn) merged 2 commits into
Conversation
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({});
```
| } | ||
| // 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. |
There was a problem hiding this comment.
Doesn't it make sense to say "Otherwise, go with the any type of the parent"? We don't seem to do any checking for the undefined type here?
There was a problem hiding this comment.
This applies to the block immediately below the comment, which checks if (!parentType). "The falsy type of the parent" would be more accurate, but isn't the intent.
Any is different in that (1) you can specify any and (2) it will override the initialiser. I decided not to comment this as it seemed pretty obvious.
| // 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; |
There was a problem hiding this comment.
If there's no parentType, and no declaration.initializer, then we return parentType when we know it does not exist - it'd probably be nicer for future references to this code if it explicitly returned undefined and had undefined as part of it's return type signature, if you ever feel like coming back to this.
Previously if the binding element had an initializer, then that type would be used. But this is incorrect:
Fixes #12620
Also get rid of a useless call to
getContextualType. I added it a year and a half ago and it just makes no sense.