Skip to content
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

Inconsistent type inference #22472

Closed
que-etc opened this issue Mar 11, 2018 · 1 comment
Closed

Inconsistent type inference #22472

que-etc opened this issue Mar 11, 2018 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@que-etc
Copy link

que-etc commented Mar 11, 2018

TypeScript Version: 2.8.0-dev.20180308

Search Terms:

  • inferred type
  • narrow

Code

const fn = (options: {foo: 'BAR'}) => {};

const fnOptions = {foo: 'BAR'};

// TS tells that "fnOptions" is not assignable to the "options" parameter.
fn(fnOptions);

// Although following invocation doesn't raise errors.
fn({foo: 'BAR'});

Expected behavior:
When passing fnOptions to fn function inferred type of its foo property should be narrowed to "BAR".

Actual behavior:
At the time of fn invocation inferred type of fnOptions is {foo: string} which renders it not assignable to the options parameter.

Playground Link: link

Related Issues: #17943

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 13, 2018
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 13, 2018

There's no guarantee at any given fn(fnOptions) call that someone hasn't written fn.foo = 'baz'; after the object was initialized. Search for "string literal widening" to see other issues on this.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants