Skip to content

Generic parameters not fully type-checked (e.g., Promise) #14770

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
stephencelis opened this issue Mar 21, 2017 · 2 comments
Closed

Generic parameters not fully type-checked (e.g., Promise) #14770

stephencelis opened this issue Mar 21, 2017 · 2 comments
Assignees
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue

Comments

@stephencelis
Copy link

stephencelis commented Mar 21, 2017

TypeScript Version: 2.2.1

Code

const a: Promise<{ foo: 'bar' }> = Promise.resolve({ foo: 'typo' });
const b: Promise<{ foo: 'bar' }> = Promise.resolve({});

Expected behavior:

  • a should type-check: it constrains foo to 'bar' but is given 'typo'.
  • b should type-check: it requires foo but is given an object without foo.

Actual behavior:

Neither type-check.

The same types check fine when resolved directly:

// Type '{ foo: "typo"; }' is not assignable to type '{ foo: "bar"; }'.
//   Types of property 'foo' are incompatible.
//     Type '"typo"' is not assignable to type '"bar"'.
const a: { foo: 'bar' } = { foo: 'typo' });

// Type '{}' is not assignable to type '{ foo: "bar"; }'.
//   Property 'foo' is missing in type '{}'.
const b: { foo: 'bar' } = {};

Generic parameter type checks fail for objects/types of different shapes:

// Type 'Promise<{ foo: string; bar: string; }>' is not assignable to type 'Promise<{ foo: "bar"; }>'.
//   Type '{ foo: string; bar: string; }' is not assignable to type '{ foo: "bar"; }'.
//     Types of property 'foo' are incompatible.
//       Type 'string' is not assignable to type '"bar"'.
const a: Promise<{ foo: 'bar' }> = Promise.resolve({ foo: 'bar', bar: 'baz' });

// Type 'Promise<number>' is not assignable to type 'Promise<{ foo: "bar"; }>'.
//   Type 'number' is not assignable to type '{ foo: "bar"; }'.
const b: Promise<{ foo: 'bar' }> = Promise.resolve(1);
@stephencelis stephencelis changed the title Promise.resolve doesn't fully type-check generic parameters Generic parameters not fully type-checked (e.g., Promise) Mar 21, 2017
@ikokostya
Copy link
Contributor

Related to #10717. See comment #10717 (comment)

@ikokostya
Copy link
Contributor

ikokostya commented Mar 23, 2017

JFI it works in flow, because type parameter in promise is declared as covariant.

@ahejlsberg ahejlsberg self-assigned this Apr 10, 2017
@ahejlsberg ahejlsberg added Design Limitation Constraints of the existing architecture prevent this from being fixed Fixed A PR has been merged for this issue labels Apr 10, 2017
@mhegazy mhegazy added this to the TypeScript 2.3.1 milestone Apr 18, 2017
@mhegazy mhegazy removed the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 18, 2017
@yuit yuit added the Breaking Change Would introduce errors in existing code label Jun 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants