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

Narrow type constraints #7422

Closed
wants to merge 2 commits into from
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
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7211,7 +7211,14 @@ namespace ts {
return getUnionType(assignableConstituents);
}
}

if (originalType.flags & TypeFlags.TypeParameter) {
const originalTypeParameter = <TypeParameter>originalType;
if (originalTypeParameter.constraint && isTypeAssignableTo(narrowedTypeCandidate, originalTypeParameter.constraint)) {
const narrowedTypeParameter = cloneTypeParameter(originalTypeParameter);
narrowedTypeParameter.constraint = narrowedTypeCandidate;
return narrowedTypeParameter;
}
}
if (isTypeAssignableTo(narrowedTypeCandidate, originalType)) {
// Narrow to the target type if it's assignable to the current type
return narrowedTypeCandidate;
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/narrowByTypeConstraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [narrowByTypeConstraint.ts]
interface Base { b }
interface Derived extends Base { d }
declare function isDerived(b: Base): b is Derived;
function f<T extends Base>(x: T, y: T) {
return isDerived(x) && isDerived(y) && x.d === y.d;
}
function g(z: Base, ka: Base) {
return isDerived(z) && isDerived(ka) && z.d === ka.d;
}


//// [narrowByTypeConstraint.js]
function f(x, y) {
return isDerived(x) && isDerived(y) && x.d === y.d;
}
function g(z, ka) {
return isDerived(z) && isDerived(ka) && z.d === ka.d;
}
58 changes: 58 additions & 0 deletions tests/baselines/reference/narrowByTypeConstraint.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
=== tests/cases/compiler/narrowByTypeConstraint.ts ===
interface Base { b }
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))
>b : Symbol(b, Decl(narrowByTypeConstraint.ts, 0, 16))

interface Derived extends Base { d }
>Derived : Symbol(Derived, Decl(narrowByTypeConstraint.ts, 0, 20))
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))
>d : Symbol(d, Decl(narrowByTypeConstraint.ts, 1, 32))

declare function isDerived(b: Base): b is Derived;
>isDerived : Symbol(isDerived, Decl(narrowByTypeConstraint.ts, 1, 36))
>b : Symbol(b, Decl(narrowByTypeConstraint.ts, 2, 27))
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))
>b : Symbol(b, Decl(narrowByTypeConstraint.ts, 2, 27))
>Derived : Symbol(Derived, Decl(narrowByTypeConstraint.ts, 0, 20))

function f<T extends Base>(x: T, y: T) {
>f : Symbol(f, Decl(narrowByTypeConstraint.ts, 2, 50))
>T : Symbol(T, Decl(narrowByTypeConstraint.ts, 3, 11))
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))
>x : Symbol(x, Decl(narrowByTypeConstraint.ts, 3, 27))
>T : Symbol(T, Decl(narrowByTypeConstraint.ts, 3, 11))
>y : Symbol(y, Decl(narrowByTypeConstraint.ts, 3, 32))
>T : Symbol(T, Decl(narrowByTypeConstraint.ts, 3, 11))

return isDerived(x) && isDerived(y) && x.d === y.d;
>isDerived : Symbol(isDerived, Decl(narrowByTypeConstraint.ts, 1, 36))
>x : Symbol(x, Decl(narrowByTypeConstraint.ts, 3, 27))
>isDerived : Symbol(isDerived, Decl(narrowByTypeConstraint.ts, 1, 36))
>y : Symbol(y, Decl(narrowByTypeConstraint.ts, 3, 32))
>x.d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>x : Symbol(x, Decl(narrowByTypeConstraint.ts, 3, 27))
>d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>y.d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>y : Symbol(y, Decl(narrowByTypeConstraint.ts, 3, 32))
>d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
}
function g(z: Base, ka: Base) {
>g : Symbol(g, Decl(narrowByTypeConstraint.ts, 5, 1))
>z : Symbol(z, Decl(narrowByTypeConstraint.ts, 6, 11))
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))
>ka : Symbol(ka, Decl(narrowByTypeConstraint.ts, 6, 19))
>Base : Symbol(Base, Decl(narrowByTypeConstraint.ts, 0, 0))

return isDerived(z) && isDerived(ka) && z.d === ka.d;
>isDerived : Symbol(isDerived, Decl(narrowByTypeConstraint.ts, 1, 36))
>z : Symbol(z, Decl(narrowByTypeConstraint.ts, 6, 11))
>isDerived : Symbol(isDerived, Decl(narrowByTypeConstraint.ts, 1, 36))
>ka : Symbol(ka, Decl(narrowByTypeConstraint.ts, 6, 19))
>z.d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>z : Symbol(z, Decl(narrowByTypeConstraint.ts, 6, 11))
>d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>ka.d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
>ka : Symbol(ka, Decl(narrowByTypeConstraint.ts, 6, 19))
>d : Symbol(Derived.d, Decl(narrowByTypeConstraint.ts, 1, 32))
}

68 changes: 68 additions & 0 deletions tests/baselines/reference/narrowByTypeConstraint.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
=== tests/cases/compiler/narrowByTypeConstraint.ts ===
interface Base { b }
>Base : Base
>b : any

interface Derived extends Base { d }
>Derived : Derived
>Base : Base
>d : any

declare function isDerived(b: Base): b is Derived;
>isDerived : (b: Base) => b is Derived
>b : Base
>Base : Base
>b : any
>Derived : Derived

function f<T extends Base>(x: T, y: T) {
>f : <T extends Base>(x: T, y: T) => boolean
>T : T
>Base : Base
>x : T
>T : T
>y : T
>T : T

return isDerived(x) && isDerived(y) && x.d === y.d;
>isDerived(x) && isDerived(y) && x.d === y.d : boolean
>isDerived(x) && isDerived(y) : boolean
>isDerived(x) : boolean
>isDerived : (b: Base) => b is Derived
>x : T
>isDerived(y) : boolean
>isDerived : (b: Base) => b is Derived
>y : T
>x.d === y.d : boolean
>x.d : any
>x : T
>d : any
>y.d : any
>y : T
>d : any
}
function g(z: Base, ka: Base) {
>g : (z: Base, ka: Base) => boolean
>z : Base
>Base : Base
>ka : Base
>Base : Base

return isDerived(z) && isDerived(ka) && z.d === ka.d;
>isDerived(z) && isDerived(ka) && z.d === ka.d : boolean
>isDerived(z) && isDerived(ka) : boolean
>isDerived(z) : boolean
>isDerived : (b: Base) => b is Derived
>z : Base
>isDerived(ka) : boolean
>isDerived : (b: Base) => b is Derived
>ka : Base
>z.d === ka.d : boolean
>z.d : any
>z : Derived
>d : any
>ka.d : any
>ka : Derived
>d : any
}

9 changes: 9 additions & 0 deletions tests/cases/compiler/narrowByTypeConstraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Base { b }
interface Derived extends Base { d }
declare function isDerived(b: Base): b is Derived;
function f<T extends Base>(x: T, y: T) {
return isDerived(x) && isDerived(y) && x.d === y.d;
}
function g(z: Base, ka: Base) {
return isDerived(z) && isDerived(ka) && z.d === ka.d;
}