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

Wrong type inference with Array.map and explicit string type / union #24352

Closed
deftomat opened this issue May 23, 2018 · 4 comments
Closed

Wrong type inference with Array.map and explicit string type / union #24352

deftomat opened this issue May 23, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@deftomat
Copy link

TypeScript Version: 2.9.0-rc

Search Terms: union string map

Code

interface Person {
  phoneNumbers: {
    __typename: 'PhoneNumber';
  }[];
}

function createPerson(): Person {
  return {
    phoneNumbers: [1].map(() => ({
      __typename: 'PhoneNumber'
    }))
  };
}

Expected behavior:
This code should pass with no errors.

Actual behavior:
Failed to compile with the following error.

Type '{ phoneNumbers: { __typename: string; }[]; }' is not assignable to type 'Person'.
  Types of property 'phoneNumbers' are incompatible.
    Type '{ __typename: string; }[]' is not assignable to type '{ __typename: "PhoneNumber"; }[]'.
      Type '{ __typename: string; }' is not assignable to type '{ __typename: "PhoneNumber"; }'.
        Types of property '__typename' are incompatible.
          Type 'string' is not assignable to type '"PhoneNumber"'.

Workaround

function createPerson(): Person {
  return {
    phoneNumbers: [1].map(() => ({
      __typename: 'PhoneNumber' as 'PhoneNumber'
    }))
  };
}
@deftomat deftomat changed the title Bad type inference with Array.map and explicit string type / union Wrong type inference with Array.map and explicit string type / union May 23, 2018
@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2018

Looks like a duplicate of #11152

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 23, 2018
@typescript-bot
Copy link
Collaborator

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

@regisbsb
Copy link

This has not been fixed and I've just run into this today

@ruairiphackett
Copy link

Still seeing this issue in 4.5.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants