Skip to content

Binding element with parent type any is of type any - #18955

Merged
Nathan Shively-Sanders (sandersn) merged 2 commits into
masterfrom
binding-element-with-parent-type-any-is-any
Oct 4, 2017
Merged

Nathan Shively-Sanders (sandersn) merged 2 commits into
masterfrom
binding-element-with-parent-type-any-is-any

Conversation

@sandersn

Copy link
Copy Markdown
Member

Previously if the binding element had an initializer, then that type would be used. But this is incorrect:

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({});

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.

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({});
```
Comment thread src/compiler/checker.ts
}
// 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sandersn
Nathan Shively-Sanders (sandersn) deleted the binding-element-with-parent-type-any-is-any branch October 4, 2017 23:03
Comment thread src/compiler/checker.ts
// 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants