Skip to content

Failure to infer subtype of string despite perfect literal match #19363

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

Closed
axefrog opened this issue Oct 20, 2017 · 2 comments
Closed

Failure to infer subtype of string despite perfect literal match #19363

axefrog opened this issue Oct 20, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@axefrog
Copy link

axefrog commented Oct 20, 2017

TypeScript Version: 2.6.0-insiders20171013 and 2.5.3

Code

interface Foo  {
  '@type': 'foo/bar';
}

class Bar<T extends Foo> {
  constructor (public value: T) { }
}

function create<T extends Foo> (value: T): Bar<T> {
  return new Bar(value);
}

function main (): Bar<Foo> {
  const value = {
    '@type': 'foo/bar'
  };
  return create(value); // ERROR
}

Expected behavior:

No errors.

Actual behavior:

Argument of type '{ '@type': string; }' is not assignable to parameter of type 'Foo'.
  Types of property ''@type'' are incompatible.
    Type 'string' is not assignable to type '"foo/bar"'.
const value: {
    '@type': string;
}

Clearly I've hardcoded the correct literal. TypeScript should not be inferring that my hardcoded literal is any old string, and should recognize that it matches the string subtype described in the interface.

Interestingly, this works:

function main (): Bar<Foo> {
  return create({
    '@type': 'foo/bar'
  });
}
@mhegazy
Copy link
Contributor

mhegazy commented Oct 20, 2017

Please see #17363 and #17480

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 20, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Nov 7, 2017
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants