Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tsc/internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3473,6 +3473,20 @@ func (r *Relater) structuredTypeRelatedToWorker(source *Type, target *Type, repo
}
return TernaryFalse
}
if r.relation == r.c.comparableRelation && source.flags&TypeFlagsTypeParameter == 0 {
// An unconstrained type parameter target could be instantiated with anything, so any
// source could overlap with it. This mirrors the source-side constraint check below;
// without it, types nested inside properties or construct signature return types (see
// issue #27910) are treated as non-overlapping on this side alone. Type parameters
// with an object constraint stay strict to preserve TS2352/TS2367 checks against
// instantiations with a different subtype of the constraint.
constraint := r.c.getConstraintOfType(target)
if constraint == nil || constraint.flags&TypeFlagsAnyOrUnknown != 0 {
if result := r.isRelatedToEx(source, r.c.unknownType, RecursionFlagsTarget, false /*reportErrors*/, nil /*headMessage*/, intersectionState); result != TernaryFalse {
return result
}
}
}
case target.flags&TypeFlagsIndexedAccess != 0:
if source.flags&TypeFlagsIndexedAccess != 0 {
// Relate components directly before falling back to constraint relationships
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
comparableRelationTypeParameterTarget.ts(29,9): error TS2367: This comparison appears to be unintentional because the types 'T' and 'U' have no overlap.


==== comparableRelationTypeParameterTarget.ts (1 errors) ====
// Reproduces #27910: a generic constructor type must be comparable with a concrete
// constructor it could overlap with. Previously, `Constructor<T> === typeof Child`
// reported TS2367 ("no overlap") even though `func(Child)` accepted `Child` as
// `Constructor<T>`.

interface Item { }

abstract class Base<T> {
get Item(): T { throw 0 }
}

class Child extends Base<Item> {
constructor(public data: any) { super() }
}

type Constructor<T> = new (data: any) => Base<T>

function func<T>(constructor: Constructor<T>): void {
if (constructor === Child) {
// do something
}
new constructor(null)
}

func(Child);

// Two unrelated type parameters still have no overlap.
function unrelated<T, U>(x: T, y: U): void {
if (x === y) {
~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'T' and 'U' have no overlap.
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/compiler/comparableRelationTypeParameterTarget.ts] ////

=== comparableRelationTypeParameterTarget.ts ===
// Reproduces #27910: a generic constructor type must be comparable with a concrete
// constructor it could overlap with. Previously, `Constructor<T> === typeof Child`
// reported TS2367 ("no overlap") even though `func(Child)` accepted `Child` as
// `Constructor<T>`.

interface Item { }
>Item : Symbol(Item, Decl(comparableRelationTypeParameterTarget.ts, 0, 0))

abstract class Base<T> {
>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 7, 20))

get Item(): T { throw 0 }
>Item : Symbol(Base.Item, Decl(comparableRelationTypeParameterTarget.ts, 7, 24))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 7, 20))
}

class Child extends Base<Item> {
>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1))
>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18))
>Item : Symbol(Item, Decl(comparableRelationTypeParameterTarget.ts, 0, 0))

constructor(public data: any) { super() }
>data : Symbol(Child.data, Decl(comparableRelationTypeParameterTarget.ts, 12, 16))
>super : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18))
}

type Constructor<T> = new (data: any) => Base<T>
>Constructor : Symbol(Constructor, Decl(comparableRelationTypeParameterTarget.ts, 13, 1))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 15, 17))
>data : Symbol(data, Decl(comparableRelationTypeParameterTarget.ts, 15, 27))
>Base : Symbol(Base, Decl(comparableRelationTypeParameterTarget.ts, 5, 18))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 15, 17))

function func<T>(constructor: Constructor<T>): void {
>func : Symbol(func, Decl(comparableRelationTypeParameterTarget.ts, 15, 48))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 17, 14))
>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17))
>Constructor : Symbol(Constructor, Decl(comparableRelationTypeParameterTarget.ts, 13, 1))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 17, 14))

if (constructor === Child) {
>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17))
>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1))

// do something
}
new constructor(null)
>constructor : Symbol(constructor, Decl(comparableRelationTypeParameterTarget.ts, 17, 17))
}

func(Child);
>func : Symbol(func, Decl(comparableRelationTypeParameterTarget.ts, 15, 48))
>Child : Symbol(Child, Decl(comparableRelationTypeParameterTarget.ts, 9, 1))

// Two unrelated type parameters still have no overlap.
function unrelated<T, U>(x: T, y: U): void {
>unrelated : Symbol(unrelated, Decl(comparableRelationTypeParameterTarget.ts, 24, 12))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 27, 19))
>U : Symbol(U, Decl(comparableRelationTypeParameterTarget.ts, 27, 21))
>x : Symbol(x, Decl(comparableRelationTypeParameterTarget.ts, 27, 25))
>T : Symbol(T, Decl(comparableRelationTypeParameterTarget.ts, 27, 19))
>y : Symbol(y, Decl(comparableRelationTypeParameterTarget.ts, 27, 30))
>U : Symbol(U, Decl(comparableRelationTypeParameterTarget.ts, 27, 21))

if (x === y) {
>x : Symbol(x, Decl(comparableRelationTypeParameterTarget.ts, 27, 25))
>y : Symbol(y, Decl(comparableRelationTypeParameterTarget.ts, 27, 30))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [tests/cases/compiler/comparableRelationTypeParameterTarget.ts] ////

=== comparableRelationTypeParameterTarget.ts ===
// Reproduces #27910: a generic constructor type must be comparable with a concrete
// constructor it could overlap with. Previously, `Constructor<T> === typeof Child`
// reported TS2367 ("no overlap") even though `func(Child)` accepted `Child` as
// `Constructor<T>`.

interface Item { }

abstract class Base<T> {
>Base : Base<T>

get Item(): T { throw 0 }
>Item : T
>0 : 0
}

class Child extends Base<Item> {
>Child : Child
>Base : Base<Item>

constructor(public data: any) { super() }
>data : any
>super() : void
>super : typeof Base
}

type Constructor<T> = new (data: any) => Base<T>
>Constructor : Constructor<T>
>data : any

function func<T>(constructor: Constructor<T>): void {
>func : <T>(constructor: Constructor<T>) => void
>constructor : Constructor<T>

if (constructor === Child) {
>constructor === Child : boolean
>constructor : Constructor<T>
>Child : typeof Child

// do something
}
new constructor(null)
>new constructor(null) : Base<T>
>constructor : Constructor<T>
}

func(Child);
>func(Child) : void
>func : <T>(constructor: Constructor<T>) => void
>Child : typeof Child

// Two unrelated type parameters still have no overlap.
function unrelated<T, U>(x: T, y: U): void {
>unrelated : <T, U>(x: T, y: U) => void
>x : T
>y : U

if (x === y) {
>x === y : boolean
>x : T
>y : U
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @noEmit: true
// @strict: true

// Reproduces #27910: a generic constructor type must be comparable with a concrete
// constructor it could overlap with. Previously, `Constructor<T> === typeof Child`
// reported TS2367 ("no overlap") even though `func(Child)` accepted `Child` as
// `Constructor<T>`.

interface Item { }

abstract class Base<T> {
get Item(): T { throw 0 }
}

class Child extends Base<Item> {
constructor(public data: any) { super() }
}

type Constructor<T> = new (data: any) => Base<T>

function func<T>(constructor: Constructor<T>): void {
if (constructor === Child) {
// do something
}
new constructor(null)
}

func(Child);

// Two unrelated type parameters still have no overlap.
function unrelated<T, U>(x: T, y: U): void {
if (x === y) {
}
}