From 284425e379451595f857f1f1a0bc466200ee6f90 Mon Sep 17 00:00:00 2001 From: z0rimo <46391780+z0rimo@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:42:12 +0900 Subject: [PATCH 1/2] Add test for nullable discriminant narrowing --- ...atedUnionNonNullAccessNarrowing.errors.txt | 82 +++++++ ...minatedUnionNonNullAccessNarrowing.symbols | 189 ++++++++++++++++ ...riminatedUnionNonNullAccessNarrowing.types | 211 ++++++++++++++++++ ...iscriminatedUnionNonNullAccessNarrowing.ts | 67 ++++++ 4 files changed, 549 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types create mode 100644 tsc/testdata/tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt new file mode 100644 index 0000000000000..c4afde6923c5f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt @@ -0,0 +1,82 @@ +discriminatedUnionNonNullAccessNarrowing.ts(24,5): error TS18048: 'small' is possibly 'undefined'. +discriminatedUnionNonNullAccessNarrowing.ts(34,21): error TS2339: Property 'type' does not exist on type 'void | { type: "1"; }'. + Property 'type' does not exist on type 'void'. +discriminatedUnionNonNullAccessNarrowing.ts(44,5): error TS18048: 'smallWithUndefinedTag' is possibly 'undefined'. +discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is possibly 'undefined'. + + +==== discriminatedUnionNonNullAccessNarrowing.ts (4 errors) ==== + // Discriminant narrowing should be consistent regardless of union size (#62511). + // Large has enough constituents to be narrowed through a constituent map; Small does not. + + type Small = + | { type: "1" } + | { type: "2" }; + + type Large = + | { type: "1" } + | { type: "2" } + | { type: "3" } + | { type: "4" } + | { type: "5" } + | { type: "6" } + | { type: "7" } + | { type: "8" } + | { type: "9" } + | { type: "10" }; + + declare const small: Small | undefined; + declare const large: Large | undefined; + + if (small!.type === "1") { + small.type; + ~~~~~ +!!! error TS18048: 'small' is possibly 'undefined'. + } + if (large!.type === "1") { + large.type; + } + + declare const smallNullOrVoid: Small | null | void; + declare const largeNullOrVoid: Large | null | void; + + if (smallNullOrVoid!.type === "1" && smallNullOrVoid !== null) { + smallNullOrVoid.type; + ~~~~ +!!! error TS2339: Property 'type' does not exist on type 'void | { type: "1"; }'. +!!! error TS2339: Property 'type' does not exist on type 'void'. + } + if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { + largeNullOrVoid.type; + } + + declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; + declare const largeWithUndefinedTag: Large | { type: undefined } | undefined; + + if (smallWithUndefinedTag?.type === undefined) { + smallWithUndefinedTag.type; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS18048: 'smallWithUndefinedTag' is possibly 'undefined'. + } + if (largeWithUndefinedTag?.type === undefined) { + largeWithUndefinedTag.type; + } + + if (small?.type === "1") { + small.type; + } + if (large?.type === "1") { + large.type; + } + + const smallTag = small?.type; + if (smallTag === "1") { + small.type; + ~~~~~ +!!! error TS18048: 'small' is possibly 'undefined'. + } + const largeTag = large?.type; + if (largeTag === "1") { + large.type; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols new file mode 100644 index 0000000000000..023f30813fc32 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols @@ -0,0 +1,189 @@ +//// [tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts] //// + +=== discriminatedUnionNonNullAccessNarrowing.ts === +// Discriminant narrowing should be consistent regardless of union size (#62511). +// Large has enough constituents to be narrowed through a constituent map; Small does not. + +type Small = +>Small : Symbol(Small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 0, 0)) + + | { type: "1" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) + + | { type: "2" }; +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) + +type Large = +>Large : Symbol(Large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 20)) + + | { type: "1" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) + + | { type: "2" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7)) + + | { type: "3" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7)) + + | { type: "4" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7)) + + | { type: "5" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7)) + + | { type: "6" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 13, 7)) + + | { type: "7" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 14, 7)) + + | { type: "8" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 15, 7)) + + | { type: "9" } +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 16, 7)) + + | { type: "10" }; +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 17, 7)) + +declare const small: Small | undefined; +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>Small : Symbol(Small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 0, 0)) + +declare const large: Large | undefined; +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>Large : Symbol(Large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 20)) + +if (small!.type === "1") { +>small!.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) + + small.type; +>small.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +} +if (large!.type === "1") { +>large!.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) + + large.type; +>large.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +} + +declare const smallNullOrVoid: Small | null | void; +>smallNullOrVoid : Symbol(smallNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 29, 13)) +>Small : Symbol(Small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 0, 0)) + +declare const largeNullOrVoid: Large | null | void; +>largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) +>Large : Symbol(Large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 20)) + +if (smallNullOrVoid!.type === "1" && smallNullOrVoid !== null) { +>smallNullOrVoid!.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) +>smallNullOrVoid : Symbol(smallNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 29, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) +>smallNullOrVoid : Symbol(smallNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 29, 13)) + + smallNullOrVoid.type; +>smallNullOrVoid : Symbol(smallNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 29, 13)) +} +if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { +>largeNullOrVoid!.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) +>largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) +>largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) + + largeNullOrVoid.type; +>largeNullOrVoid.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +>largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +} + +declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; +>smallWithUndefinedTag : Symbol(smallWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 13)) +>Small : Symbol(Small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 0, 0)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 46)) + +declare const largeWithUndefinedTag: Large | { type: undefined } | undefined; +>largeWithUndefinedTag : Symbol(largeWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 13)) +>Large : Symbol(Large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 20)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 46)) + +if (smallWithUndefinedTag?.type === undefined) { +>smallWithUndefinedTag?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 46)) +>smallWithUndefinedTag : Symbol(smallWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 46)) +>undefined : Symbol(undefined) + + smallWithUndefinedTag.type; +>smallWithUndefinedTag.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 46)) +>smallWithUndefinedTag : Symbol(smallWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 39, 46)) +} +if (largeWithUndefinedTag?.type === undefined) { +>largeWithUndefinedTag?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 6 more) +>largeWithUndefinedTag : Symbol(largeWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 6 more) +>undefined : Symbol(undefined) + + largeWithUndefinedTag.type; +>largeWithUndefinedTag.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 46)) +>largeWithUndefinedTag : Symbol(largeWithUndefinedTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 40, 46)) +} + +if (small?.type === "1") { +>small?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) + + small.type; +>small.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +} +if (large?.type === "1") { +>large?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) + + large.type; +>large.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +} + +const smallTag = small?.type; +>smallTag : Symbol(smallTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 56, 5)) +>small?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 5, 7)) + +if (smallTag === "1") { +>smallTag : Symbol(smallTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 56, 5)) + + small.type; +>small.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +>small : Symbol(small, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 19, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 4, 7)) +} +const largeTag = large?.type; +>largeTag : Symbol(largeTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 60, 5)) +>large?.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 9, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 10, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 11, 7), Decl(discriminatedUnionNonNullAccessNarrowing.ts, 12, 7) ... and 5 more) + +if (largeTag === "1") { +>largeTag : Symbol(largeTag, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 60, 5)) + + large.type; +>large.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +>large : Symbol(large, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 20, 13)) +>type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types new file mode 100644 index 0000000000000..f7a426b415158 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types @@ -0,0 +1,211 @@ +//// [tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts] //// + +=== discriminatedUnionNonNullAccessNarrowing.ts === +// Discriminant narrowing should be consistent regardless of union size (#62511). +// Large has enough constituents to be narrowed through a constituent map; Small does not. + +type Small = +>Small : Small + + | { type: "1" } +>type : "1" + + | { type: "2" }; +>type : "2" + +type Large = +>Large : Large + + | { type: "1" } +>type : "1" + + | { type: "2" } +>type : "2" + + | { type: "3" } +>type : "3" + + | { type: "4" } +>type : "4" + + | { type: "5" } +>type : "5" + + | { type: "6" } +>type : "6" + + | { type: "7" } +>type : "7" + + | { type: "8" } +>type : "8" + + | { type: "9" } +>type : "9" + + | { type: "10" }; +>type : "10" + +declare const small: Small | undefined; +>small : Small | undefined + +declare const large: Large | undefined; +>large : Large | undefined + +if (small!.type === "1") { +>small!.type === "1" : boolean +>small!.type : "1" | "2" +>small! : Small +>small : Small | undefined +>type : "1" | "2" +>"1" : "1" + + small.type; +>small.type : "1" +>small : { type: "1"; } | undefined +>type : "1" +} +if (large!.type === "1") { +>large!.type === "1" : boolean +>large!.type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" +>large! : Large +>large : Large | undefined +>type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" +>"1" : "1" + + large.type; +>large.type : "1" +>large : { type: "1"; } +>type : "1" +} + +declare const smallNullOrVoid: Small | null | void; +>smallNullOrVoid : void | Small | null + +declare const largeNullOrVoid: Large | null | void; +>largeNullOrVoid : void | Large | null + +if (smallNullOrVoid!.type === "1" && smallNullOrVoid !== null) { +>smallNullOrVoid!.type === "1" && smallNullOrVoid !== null : boolean +>smallNullOrVoid!.type === "1" : boolean +>smallNullOrVoid!.type : "1" | "2" +>smallNullOrVoid! : Small +>smallNullOrVoid : void | Small | null +>type : "1" | "2" +>"1" : "1" +>smallNullOrVoid !== null : boolean +>smallNullOrVoid : void | { type: "1"; } | null + + smallNullOrVoid.type; +>smallNullOrVoid.type : any +>smallNullOrVoid : void | { type: "1"; } +>type : any +} +if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { +>largeNullOrVoid!.type === "1" && largeNullOrVoid !== null : boolean +>largeNullOrVoid!.type === "1" : boolean +>largeNullOrVoid!.type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" +>largeNullOrVoid! : Large +>largeNullOrVoid : void | Large | null +>type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" +>"1" : "1" +>largeNullOrVoid !== null : boolean +>largeNullOrVoid : { type: "1"; } + + largeNullOrVoid.type; +>largeNullOrVoid.type : "1" +>largeNullOrVoid : { type: "1"; } +>type : "1" +} + +declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; +>smallWithUndefinedTag : { type: undefined; } | Small | undefined +>type : undefined + +declare const largeWithUndefinedTag: Large | { type: undefined } | undefined; +>largeWithUndefinedTag : { type: undefined; } | Large | undefined +>type : undefined + +if (smallWithUndefinedTag?.type === undefined) { +>smallWithUndefinedTag?.type === undefined : boolean +>smallWithUndefinedTag?.type : "1" | "2" | undefined +>smallWithUndefinedTag : { type: undefined; } | Small | undefined +>type : "1" | "2" | undefined +>undefined : undefined + + smallWithUndefinedTag.type; +>smallWithUndefinedTag.type : undefined +>smallWithUndefinedTag : { type: undefined; } | undefined +>type : undefined +} +if (largeWithUndefinedTag?.type === undefined) { +>largeWithUndefinedTag?.type === undefined : boolean +>largeWithUndefinedTag?.type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>largeWithUndefinedTag : { type: undefined; } | Large | undefined +>type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>undefined : undefined + + largeWithUndefinedTag.type; +>largeWithUndefinedTag.type : undefined +>largeWithUndefinedTag : { type: undefined; } +>type : undefined +} + +if (small?.type === "1") { +>small?.type === "1" : boolean +>small?.type : "1" | "2" | undefined +>small : Small | undefined +>type : "1" | "2" | undefined +>"1" : "1" + + small.type; +>small.type : "1" +>small : { type: "1"; } +>type : "1" +} +if (large?.type === "1") { +>large?.type === "1" : boolean +>large?.type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>large : Large | undefined +>type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>"1" : "1" + + large.type; +>large.type : "1" +>large : { type: "1"; } +>type : "1" +} + +const smallTag = small?.type; +>smallTag : "1" | "2" | undefined +>small?.type : "1" | "2" | undefined +>small : Small | undefined +>type : "1" | "2" | undefined + +if (smallTag === "1") { +>smallTag === "1" : boolean +>smallTag : "1" | "2" | undefined +>"1" : "1" + + small.type; +>small.type : "1" +>small : { type: "1"; } | undefined +>type : "1" +} +const largeTag = large?.type; +>largeTag : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>large?.type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>large : Large | undefined +>type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined + +if (largeTag === "1") { +>largeTag === "1" : boolean +>largeTag : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | undefined +>"1" : "1" + + large.type; +>large.type : "1" +>large : { type: "1"; } +>type : "1" +} + diff --git a/tsc/testdata/tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts b/tsc/testdata/tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts new file mode 100644 index 0000000000000..4746fd04b1482 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts @@ -0,0 +1,67 @@ +// @strict: true +// @noEmit: true + +// Discriminant narrowing should be consistent regardless of union size (#62511). +// Large has enough constituents to be narrowed through a constituent map; Small does not. + +type Small = + | { type: "1" } + | { type: "2" }; + +type Large = + | { type: "1" } + | { type: "2" } + | { type: "3" } + | { type: "4" } + | { type: "5" } + | { type: "6" } + | { type: "7" } + | { type: "8" } + | { type: "9" } + | { type: "10" }; + +declare const small: Small | undefined; +declare const large: Large | undefined; + +if (small!.type === "1") { + small.type; +} +if (large!.type === "1") { + large.type; +} + +declare const smallNullOrVoid: Small | null | void; +declare const largeNullOrVoid: Large | null | void; + +if (smallNullOrVoid!.type === "1" && smallNullOrVoid !== null) { + smallNullOrVoid.type; +} +if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { + largeNullOrVoid.type; +} + +declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; +declare const largeWithUndefinedTag: Large | { type: undefined } | undefined; + +if (smallWithUndefinedTag?.type === undefined) { + smallWithUndefinedTag.type; +} +if (largeWithUndefinedTag?.type === undefined) { + largeWithUndefinedTag.type; +} + +if (small?.type === "1") { + small.type; +} +if (large?.type === "1") { + large.type; +} + +const smallTag = small?.type; +if (smallTag === "1") { + small.type; +} +const largeTag = large?.type; +if (largeTag === "1") { + large.type; +} From 537de844cb57e98634b28b5d1951f6492c356bf5 Mon Sep 17 00:00:00 2001 From: z0rimo <46391780+z0rimo@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:54:11 +0900 Subject: [PATCH 2/2] Fix nullable discriminant narrowing consistency --- tsc/internal/checker/flow.go | 8 ++++++++ ...minatedUnionNonNullAccessNarrowing.errors.txt | 16 +++++++++++++++- ...criminatedUnionNonNullAccessNarrowing.symbols | 2 -- ...iscriminatedUnionNonNullAccessNarrowing.types | 14 +++++++------- .../compiler/narrowingUnionWithBang.types | 4 ++-- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/tsc/internal/checker/flow.go b/tsc/internal/checker/flow.go index 8d14ca5258079..b875db81218e5 100644 --- a/tsc/internal/checker/flow.go +++ b/tsc/internal/checker/flow.go @@ -707,6 +707,14 @@ func (c *Checker) narrowTypeByDiscriminantProperty(t *Type, access *ast.Node, op candidate := c.getConstituentTypeForKeyType(t, c.getTypeOfExpression(value)) if candidate != nil { if assumeTrue && operator == ast.KindEqualsEqualsEqualsToken || !assumeTrue && operator == ast.KindExclamationEqualsEqualsToken { + // Nullable and void constituents have no key property, so they are absent from the constituent + // map, but the general discriminant narrowing logic keeps them. Preserve them as well, checking + // membership by binary search so that the fast path stays sublinear. + for _, nullableType := range []*Type{c.undefinedType, c.missingType, c.optionalType, c.nullType, c.voidType} { + if containsType(t.Types(), nullableType) { + candidate = c.getUnionType([]*Type{candidate, nullableType}) + } + } return candidate } if propType := c.getTypeOfPropertyOfType(candidate, keyPropertyName); propType != nil && isUnitType(propType) { diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt index c4afde6923c5f..f02f9e66f35a2 100644 --- a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.errors.txt @@ -1,11 +1,16 @@ discriminatedUnionNonNullAccessNarrowing.ts(24,5): error TS18048: 'small' is possibly 'undefined'. +discriminatedUnionNonNullAccessNarrowing.ts(27,5): error TS18048: 'large' is possibly 'undefined'. discriminatedUnionNonNullAccessNarrowing.ts(34,21): error TS2339: Property 'type' does not exist on type 'void | { type: "1"; }'. Property 'type' does not exist on type 'void'. +discriminatedUnionNonNullAccessNarrowing.ts(37,21): error TS2339: Property 'type' does not exist on type 'void | { type: "1"; }'. + Property 'type' does not exist on type 'void'. discriminatedUnionNonNullAccessNarrowing.ts(44,5): error TS18048: 'smallWithUndefinedTag' is possibly 'undefined'. +discriminatedUnionNonNullAccessNarrowing.ts(47,5): error TS18048: 'largeWithUndefinedTag' is possibly 'undefined'. discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is possibly 'undefined'. +discriminatedUnionNonNullAccessNarrowing.ts(63,5): error TS18048: 'large' is possibly 'undefined'. -==== discriminatedUnionNonNullAccessNarrowing.ts (4 errors) ==== +==== discriminatedUnionNonNullAccessNarrowing.ts (8 errors) ==== // Discriminant narrowing should be consistent regardless of union size (#62511). // Large has enough constituents to be narrowed through a constituent map; Small does not. @@ -35,6 +40,8 @@ discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is pos } if (large!.type === "1") { large.type; + ~~~~~ +!!! error TS18048: 'large' is possibly 'undefined'. } declare const smallNullOrVoid: Small | null | void; @@ -48,6 +55,9 @@ discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is pos } if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { largeNullOrVoid.type; + ~~~~ +!!! error TS2339: Property 'type' does not exist on type 'void | { type: "1"; }'. +!!! error TS2339: Property 'type' does not exist on type 'void'. } declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; @@ -60,6 +70,8 @@ discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is pos } if (largeWithUndefinedTag?.type === undefined) { largeWithUndefinedTag.type; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS18048: 'largeWithUndefinedTag' is possibly 'undefined'. } if (small?.type === "1") { @@ -78,5 +90,7 @@ discriminatedUnionNonNullAccessNarrowing.ts(59,5): error TS18048: 'small' is pos const largeTag = large?.type; if (largeTag === "1") { large.type; + ~~~~~ +!!! error TS18048: 'large' is possibly 'undefined'. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols index 023f30813fc32..c1916fccb468b 100644 --- a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.symbols @@ -99,9 +99,7 @@ if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { >largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) largeNullOrVoid.type; ->largeNullOrVoid.type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) >largeNullOrVoid : Symbol(largeNullOrVoid, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 30, 13)) ->type : Symbol(type, Decl(discriminatedUnionNonNullAccessNarrowing.ts, 8, 7)) } declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; diff --git a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types index f7a426b415158..4c917b2bd8a5f 100644 --- a/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types +++ b/tsc/testdata/baselines/reference/compiler/discriminatedUnionNonNullAccessNarrowing.types @@ -75,7 +75,7 @@ if (large!.type === "1") { large.type; >large.type : "1" ->large : { type: "1"; } +>large : { type: "1"; } | undefined >type : "1" } @@ -110,12 +110,12 @@ if (largeNullOrVoid!.type === "1" && largeNullOrVoid !== null) { >type : "1" | "10" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" >"1" : "1" >largeNullOrVoid !== null : boolean ->largeNullOrVoid : { type: "1"; } +>largeNullOrVoid : void | { type: "1"; } | null largeNullOrVoid.type; ->largeNullOrVoid.type : "1" ->largeNullOrVoid : { type: "1"; } ->type : "1" +>largeNullOrVoid.type : any +>largeNullOrVoid : void | { type: "1"; } +>type : any } declare const smallWithUndefinedTag: Small | { type: undefined } | undefined; @@ -147,7 +147,7 @@ if (largeWithUndefinedTag?.type === undefined) { largeWithUndefinedTag.type; >largeWithUndefinedTag.type : undefined ->largeWithUndefinedTag : { type: undefined; } +>largeWithUndefinedTag : { type: undefined; } | undefined >type : undefined } @@ -205,7 +205,7 @@ if (largeTag === "1") { large.type; >large.type : "1" ->large : { type: "1"; } +>large : { type: "1"; } | undefined >type : "1" } diff --git a/tsc/testdata/baselines/reference/compiler/narrowingUnionWithBang.types b/tsc/testdata/baselines/reference/compiler/narrowingUnionWithBang.types index 0e44a18ee2844..3470ec943bbb3 100644 --- a/tsc/testdata/baselines/reference/compiler/narrowingUnionWithBang.types +++ b/tsc/testdata/baselines/reference/compiler/narrowingUnionWithBang.types @@ -83,9 +83,9 @@ if (working.thing!.name !== "Correct") { >log : (...data: any[]) => void >working.thing!.id : string >working.thing! : { name: 'Correct'; id: string; } ->working.thing : { name: 'Correct'; id: string; } +>working.thing : { name: 'Correct'; id: string; } | undefined >working : WorkingType ->thing : { name: 'Correct'; id: string; } +>thing : { name: 'Correct'; id: string; } | undefined >id : string }