Skip to content

Commit

Permalink
Use bidirectional comparability (aka comparability) in narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Sep 1, 2022
1 parent 19defbf commit 46ce0b2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25096,7 +25096,7 @@ namespace ts {
const narrowedPropType = narrowType(propType);
return filterType(type, t => {
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
return !(narrowedPropType.flags & TypeFlags.Never) && isTypeComparableTo(narrowedPropType, discriminantType);
return !(narrowedPropType.flags & TypeFlags.Never) && areTypesComparable(narrowedPropType, discriminantType);
});
}

Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/50527.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [50527.ts]
type S =
| { type: 'string', value: string }
| { type: 'number', value: number }
| { type: 'unknown', value: unknown }
| { value: undefined };

declare var s: S

if (s.value !== undefined) {
s;
}


//// [50527.js]
"use strict";
if (s.value !== undefined) {
s;
}
33 changes: 33 additions & 0 deletions tests/baselines/reference/50527.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/50527.ts ===
type S =
>S : Symbol(S, Decl(50527.ts, 0, 0))

| { type: 'string', value: string }
>type : Symbol(type, Decl(50527.ts, 1, 3))
>value : Symbol(value, Decl(50527.ts, 1, 19))

| { type: 'number', value: number }
>type : Symbol(type, Decl(50527.ts, 2, 3))
>value : Symbol(value, Decl(50527.ts, 2, 19))

| { type: 'unknown', value: unknown }
>type : Symbol(type, Decl(50527.ts, 3, 3))
>value : Symbol(value, Decl(50527.ts, 3, 20))

| { value: undefined };
>value : Symbol(value, Decl(50527.ts, 4, 3))

declare var s: S
>s : Symbol(s, Decl(50527.ts, 6, 11))
>S : Symbol(S, Decl(50527.ts, 0, 0))

if (s.value !== undefined) {
>s.value : Symbol(value, Decl(50527.ts, 1, 19), Decl(50527.ts, 2, 19), Decl(50527.ts, 3, 20), Decl(50527.ts, 4, 3))
>s : Symbol(s, Decl(50527.ts, 6, 11))
>value : Symbol(value, Decl(50527.ts, 1, 19), Decl(50527.ts, 2, 19), Decl(50527.ts, 3, 20), Decl(50527.ts, 4, 3))
>undefined : Symbol(undefined)

s;
>s : Symbol(s, Decl(50527.ts, 6, 11))
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/50527.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/50527.ts ===
type S =
>S : { type: 'string'; value: string; } | { type: 'number'; value: number; } | { type: 'unknown'; value: unknown; } | { value: undefined; }

| { type: 'string', value: string }
>type : "string"
>value : string

| { type: 'number', value: number }
>type : "number"
>value : number

| { type: 'unknown', value: unknown }
>type : "unknown"
>value : unknown

| { value: undefined };
>value : undefined

declare var s: S
>s : S

if (s.value !== undefined) {
>s.value !== undefined : boolean
>s.value : unknown
>s : S
>value : unknown
>undefined : undefined

s;
>s : { type: "string"; value: string; } | { type: "number"; value: number; } | { type: "unknown"; value: unknown; }
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/50527.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true

type S =
| { type: 'string', value: string }
| { type: 'number', value: number }
| { type: 'unknown', value: unknown }
| { value: undefined };

declare var s: S

if (s.value !== undefined) {
s;
}

0 comments on commit 46ce0b2

Please sign in to comment.