Skip to content

Fix #33448 - Filter discriminants of type never #33498

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

Closed
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 @@ -17363,7 +17363,14 @@ namespace ts {
}
const propType = getTypeOfPropertyOfType(type, propName);
const narrowedPropType = propType && narrowType(propType);
return propType === narrowedPropType ? type : filterType(type, t => isTypeComparableTo(getTypeOfPropertyOrIndexSignature(t, propName), narrowedPropType!));
if (propType === narrowedPropType) {
return type;
}
const isPropertyDiscriminated = (t: Type) => {
const constituentPropType = getTypeOfPropertyOrIndexSignature(t, propName);
return (constituentPropType.flags & TypeFlags.Never) === 0 && isTypeComparableTo(constituentPropType, narrowedPropType!);
};
return filterType(type, isPropertyDiscriminated);
}

function narrowTypeByTruthiness(type: Type, expr: Expression, assumeTrue: boolean): Type {
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/discriminatedUnionTypes2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,27 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS
foo;
}
}

// Repro from #33448

type a = {
type: 'a',
data: string
}
type b = {
type: 'b',
name: string
}
type c = {
type: 'c',
other: string
}
type abc = a | b | c;
function f(problem: abc & (b | c)) {
if (problem.type === 'b') {
console.log(problem.name);
} else {
console.log(problem.other);
}
}

31 changes: 31 additions & 0 deletions tests/baselines/reference/discriminatedUnionTypes2.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ function f31(foo: Foo) {
foo;
}
}

// Repro from #33448

type a = {
type: 'a',
data: string
}
type b = {
type: 'b',
name: string
}
type c = {
type: 'c',
other: string
}
type abc = a | b | c;
function f(problem: abc & (b | c)) {
if (problem.type === 'b') {
console.log(problem.name);
} else {
console.log(problem.other);
}
}


//// [discriminatedUnionTypes2.js]
Expand Down Expand Up @@ -164,3 +187,11 @@ function f31(foo) {
foo;
}
}
function f(problem) {
if (problem.type === 'b') {
console.log(problem.name);
}
else {
console.log(problem.other);
}
}
66 changes: 66 additions & 0 deletions tests/baselines/reference/discriminatedUnionTypes2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,69 @@ function f31(foo: Foo) {
}
}

// Repro from #33448

type a = {
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 93, 1))

type: 'a',
>type : Symbol(type, Decl(discriminatedUnionTypes2.ts, 97, 10))

data: string
>data : Symbol(data, Decl(discriminatedUnionTypes2.ts, 98, 14))
}
type b = {
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 100, 1))

type: 'b',
>type : Symbol(type, Decl(discriminatedUnionTypes2.ts, 101, 10))

name: string
>name : Symbol(name, Decl(discriminatedUnionTypes2.ts, 102, 14))
}
type c = {
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 104, 1))

type: 'c',
>type : Symbol(type, Decl(discriminatedUnionTypes2.ts, 105, 10))

other: string
>other : Symbol(other, Decl(discriminatedUnionTypes2.ts, 106, 14))
}
type abc = a | b | c;
>abc : Symbol(abc, Decl(discriminatedUnionTypes2.ts, 108, 1))
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 93, 1))
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 100, 1))
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 104, 1))

function f(problem: abc & (b | c)) {
>f : Symbol(f, Decl(discriminatedUnionTypes2.ts, 109, 21))
>problem : Symbol(problem, Decl(discriminatedUnionTypes2.ts, 110, 11))
>abc : Symbol(abc, Decl(discriminatedUnionTypes2.ts, 108, 1))
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 100, 1))
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 104, 1))

if (problem.type === 'b') {
>problem.type : Symbol(type, Decl(discriminatedUnionTypes2.ts, 101, 10), Decl(discriminatedUnionTypes2.ts, 105, 10), Decl(discriminatedUnionTypes2.ts, 97, 10), Decl(discriminatedUnionTypes2.ts, 101, 10), Decl(discriminatedUnionTypes2.ts, 97, 10) ... and 5 more)
>problem : Symbol(problem, Decl(discriminatedUnionTypes2.ts, 110, 11))
>type : Symbol(type, Decl(discriminatedUnionTypes2.ts, 101, 10), Decl(discriminatedUnionTypes2.ts, 105, 10), Decl(discriminatedUnionTypes2.ts, 97, 10), Decl(discriminatedUnionTypes2.ts, 101, 10), Decl(discriminatedUnionTypes2.ts, 97, 10) ... and 5 more)

console.log(problem.name);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>problem.name : Symbol(name, Decl(discriminatedUnionTypes2.ts, 102, 14))
>problem : Symbol(problem, Decl(discriminatedUnionTypes2.ts, 110, 11))
>name : Symbol(name, Decl(discriminatedUnionTypes2.ts, 102, 14))

} else {
console.log(problem.other);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>problem.other : Symbol(other, Decl(discriminatedUnionTypes2.ts, 106, 14))
>problem : Symbol(problem, Decl(discriminatedUnionTypes2.ts, 110, 11))
>other : Symbol(other, Decl(discriminatedUnionTypes2.ts, 106, 14))
}
}

64 changes: 64 additions & 0 deletions tests/baselines/reference/discriminatedUnionTypes2.types
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,67 @@ function f31(foo: Foo) {
}
}

// Repro from #33448

type a = {
>a : a

type: 'a',
>type : "a"

data: string
>data : string
}
type b = {
>b : b

type: 'b',
>type : "b"

name: string
>name : string
}
type c = {
>c : c

type: 'c',
>type : "c"

other: string
>other : string
}
type abc = a | b | c;
>abc : abc

function f(problem: abc & (b | c)) {
>f : (problem: b | c | (a & b) | (a & c) | (b & c) | (c & b)) => void
>problem : b | c | (a & b) | (a & c) | (b & c) | (c & b)

if (problem.type === 'b') {
>problem.type === 'b' : boolean
>problem.type : "b" | "c"
>problem : b | c | (a & b) | (a & c) | (b & c) | (c & b)
>type : "b" | "c"
>'b' : "b"

console.log(problem.name);
>console.log(problem.name) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>problem.name : string
>problem : b
>name : string

} else {
console.log(problem.other);
>console.log(problem.other) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>problem.other : string
>problem : c
>other : string
}
}

23 changes: 23 additions & 0 deletions tests/cases/conformance/types/union/discriminatedUnionTypes2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,26 @@ function f31(foo: Foo) {
foo;
}
}

// Repro from #33448

type a = {
type: 'a',
data: string
}
type b = {
type: 'b',
name: string
}
type c = {
type: 'c',
other: string
}
type abc = a | b | c;
function f(problem: abc & (b | c)) {
if (problem.type === 'b') {
console.log(problem.name);
} else {
console.log(problem.other);
}
}