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

constraint check failure if a type that satisfies the constraint is used in the generic parameter default #31159

Closed
ashidaharo opened this issue Apr 29, 2019 · 1 comment · Fixed by #31240
Assignees
Labels
Bug A bug in TypeScript

Comments

@ashidaharo
Copy link

TypeScript Version: 3.4.4

Search Terms:
generic default type

Code

// tricky interface
interface Settable<T, V> {
  set(value: V): T;
}

// implement
class Identity<V> implments Settable<Identity<V>, V> {
  readonly item: V;
  constructor(value: V) {
    this.item = value;
  }
  public set(value: V): Identity<V> {
    return new Identity<V>(value);
  }
}

// generic parameter default
interface Test1<V, T extends Settable<T, V> = Identity<V>> { };
let test1: Test1<number>;

// not generic parameter default
interface Test2Base<V, T extends Settable<T, V>> { };
type Test2<V> = Test2Base<V, Identity<V>>;
let test2: Test2<number>;

Expected behavior:
For certain types X and Y, the constraint "X extends Y" is either correct or incorrect and not both or neither.
Both interface Test 1 and type Test 2 contain the constraint “Identity extends Settable <Identity , V> for a type V” and no other constraints.
Therefore, both Test1 and Test2 should either meet or not meet the previous constraints.
Actual behavior:
Test1 is an error "Type 'Identity' does not satisfy the constraint 'Settable<T, V>'."
But, Test2 isn't an error.
Type constraint determination may change depending on whether the type is Generic Parameter Default.
Playground Link:
https://www.typescriptlang.org/play/#src=interface%20Settable%3CT%2C%20V%3E%20%7B%0D%0A%20%20%20%20set(value%3A%20V)%3A%20T%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Identity%3CV%3E%20%7B%0D%0A%20%20%20%20readonly%20item%3A%20V%3B%0D%0A%20%20%20%20constructor(value%3A%20V)%20%7B%0D%0A%20%20%20%20%20%20%20%20this.item%20%3D%20value%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20set(value%3A%20V)%3A%20Identity%3CV%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20new%20Identity%3CV%3E(value)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20Test1%3CV%2C%20T%20extends%20Settable%3CT%2C%20V%3E%20%3D%20Identity%3CV%3E%3E%20%7B%20%7D%3B%0D%0Alet%20test1%3A%20Test1%3Cnumber%3E%3B%0D%0A%0D%0Ainterface%20Test2Base%3CV%2C%20T%20extends%20Settable%3CT%2C%20V%3E%3E%20%7B%20%7D%3B%0D%0Atype%20Test2%3CV%3E%20%3D%20Test2Base%3CV%2C%20Identity%3CV%3E%3E%3B%0D%0Alet%20test2%3A%20Test2%3Cnumber%3E%3B

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 29, 2019
@weswigham
Copy link
Member

Looks like a simple bug to fix - we fail to instantiate Settable<T, V> with T = Identity<V> when checking if the default (Identity<V>) is assignable to the constraint. PR is up at #31240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants