diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e911d7fe7d2a3..7b4b98217ce4a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); }); } diff --git a/tests/baselines/reference/50527.js b/tests/baselines/reference/50527.js new file mode 100644 index 0000000000000..464861a224d1e --- /dev/null +++ b/tests/baselines/reference/50527.js @@ -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; +} diff --git a/tests/baselines/reference/50527.symbols b/tests/baselines/reference/50527.symbols new file mode 100644 index 0000000000000..82e4f791e50aa --- /dev/null +++ b/tests/baselines/reference/50527.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/50527.types b/tests/baselines/reference/50527.types new file mode 100644 index 0000000000000..aef6d2fea0f85 --- /dev/null +++ b/tests/baselines/reference/50527.types @@ -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; } +} + diff --git a/tests/cases/compiler/50527.ts b/tests/cases/compiler/50527.ts new file mode 100644 index 0000000000000..19f1753b421c2 --- /dev/null +++ b/tests/cases/compiler/50527.ts @@ -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; +}