Description
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: