|
| 1 | +//// [tests/cases/compiler/comparableRelationTypeParameterTarget.ts] //// |
| 2 | + |
| 3 | +=== comparableRelationTypeParameterTarget.ts === |
| 4 | +// Reproduces #27910: a generic constructor type must be comparable with a concrete |
| 5 | +// constructor it could overlap with. Previously, `Constructor<T> === typeof Child` |
| 6 | +// reported TS2367 ("no overlap") even though `func(Child)` accepted `Child` as |
| 7 | +// `Constructor<T>`. |
| 8 | + |
| 9 | +interface Item { } |
| 10 | +>Item : Symbol(Item, Decl(comparableRelationTypeParameterTarget.ts, 0, 0)) |
| 11 | + |
| 12 | +abstract class Base<T> { |
| 13 | +>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18)) |
| 14 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 7, 20)) |
| 15 | + |
| 16 | + get Item(): T { throw 0 } |
| 17 | +>Item : Symbol(Base.Item, Decl(comparableRelationTypeParameterTarget.ts, 7, 24)) |
| 18 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 7, 20)) |
| 19 | +} |
| 20 | + |
| 21 | +class Child extends Base<Item> { |
| 22 | +>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1)) |
| 23 | +>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18)) |
| 24 | +>Item : Symbol(Item, Decl(comparableRelationTypeParameterTarget.ts, 0, 0)) |
| 25 | + |
| 26 | + constructor(public data: any) { super() } |
| 27 | +>data : Symbol(Child.data, Decl(comparableRelationTypeParameterTarget.ts, 12, 16)) |
| 28 | +>super : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18)) |
| 29 | +} |
| 30 | + |
| 31 | +type Constructor<T> = new (data: any) => Base<T> |
| 32 | +>Constructor : Symbol(Constructor, Decl(comparableRelationTypeParameterTarget.ts, 13, 1)) |
| 33 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 15, 17)) |
| 34 | +>data : Symbol(data, Decl(comparableRelationTypeParameterTarget.ts, 15, 27)) |
| 35 | +>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18)) |
| 36 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 15, 17)) |
| 37 | + |
| 38 | +function func<T>(constructor: Constructor<T>): void { |
| 39 | +>func : Symbol(func, Decl(comparableRelationTypeParameterTarget.ts, 15, 48)) |
| 40 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 17, 14)) |
| 41 | +>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17)) |
| 42 | +>Constructor : Symbol(Constructor, Decl(comparableRelationTypeParameterTarget.ts, 13, 1)) |
| 43 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 17, 14)) |
| 44 | + |
| 45 | + if (constructor === Child) { |
| 46 | +>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17)) |
| 47 | +>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1)) |
| 48 | + |
| 49 | + // do something |
| 50 | + } |
| 51 | + new constructor(null) |
| 52 | +>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17)) |
| 53 | +} |
| 54 | + |
| 55 | +func(Child); |
| 56 | +>func : Symbol(func, Decl(comparableRelationTypeParameterTarget.ts, 15, 48)) |
| 57 | +>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1)) |
| 58 | + |
| 59 | +// Two unrelated type parameters still have no overlap. |
| 60 | +function unrelated<T, U>(x: T, y: U): void { |
| 61 | +>unrelated : Symbol(unrelated, Decl(comparableRelationTypeParameterTarget.ts, 24, 12)) |
| 62 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 27, 19)) |
| 63 | +>U : Symbol(U, Decl(comparableRelationTypeParameterTarget.ts, 27, 21)) |
| 64 | +>x : Symbol(x, Decl(comparableRelationTypeParameterTarget.ts, 27, 25)) |
| 65 | +>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 27, 19)) |
| 66 | +>y : Symbol(y, Decl(comparableRelationTypeParameterTarget.ts, 27, 30)) |
| 67 | +>U : Symbol(U, Decl(comparableRelationTypeParameterTarget.ts, 27, 21)) |
| 68 | + |
| 69 | + if (x === y) { |
| 70 | +>x : Symbol(x, Decl(comparableRelationTypeParameterTarget.ts, 27, 25)) |
| 71 | +>y : Symbol(y, Decl(comparableRelationTypeParameterTarget.ts, 27, 30)) |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments