From 6257f084790d61fd50c66bb18c0d87f3ab2a37cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 26 Oct 2026 13:12:19 +0100 Subject: [PATCH] Add a regression test for narrowing NoInfer union constituents of non-generic aliases --- .../compiler/narrowingNoInfer2.symbols | 77 +++++++++++++++++++ .../compiler/narrowingNoInfer2.types | 71 +++++++++++++++++ .../tests/cases/compiler/narrowingNoInfer2.ts | 26 +++++++ 3 files changed, 174 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.types create mode 100644 tsc/testdata/tests/cases/compiler/narrowingNoInfer2.ts diff --git a/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.symbols b/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.symbols new file mode 100644 index 0000000000000..62ad300d4e6c5 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.symbols @@ -0,0 +1,77 @@ +//// [tests/cases/compiler/narrowingNoInfer2.ts] //// + +=== narrowingNoInfer2.ts === +type U1 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; +>U1 : Symbol(U1, Decl(narrowingNoInfer2.ts, 0, 0)) +>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 0, 19)) +>a : Symbol(a, Decl(narrowingNoInfer2.ts, 0, 30)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 0, 46)) +>b : Symbol(b, Decl(narrowingNoInfer2.ts, 0, 57)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 0, 74)) + +function f1(u: U1) { +>f1 : Symbol(f1, Decl(narrowingNoInfer2.ts, 0, 87)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 2, 12)) +>U1 : Symbol(U1, Decl(narrowingNoInfer2.ts, 0, 0)) + + if (u.kind === "a") { +>u.kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 0, 19), Decl(narrowingNoInfer2.ts, 0, 46), Decl(narrowingNoInfer2.ts, 0, 74)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 2, 12)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 0, 19), Decl(narrowingNoInfer2.ts, 0, 46), Decl(narrowingNoInfer2.ts, 0, 74)) + + u.a; +>u.a : Symbol(a, Decl(narrowingNoInfer2.ts, 0, 30)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 2, 12)) +>a : Symbol(a, Decl(narrowingNoInfer2.ts, 0, 30)) + } +} + +type U2 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; +>U2 : Symbol(U2, Decl(narrowingNoInfer2.ts, 6, 1)) +>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 8, 19)) +>a : Symbol(a, Decl(narrowingNoInfer2.ts, 8, 30)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 8, 46)) +>b : Symbol(b, Decl(narrowingNoInfer2.ts, 8, 57)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 8, 74)) + +interface Box { +>Box : Symbol(Box, Decl(narrowingNoInfer2.ts, 8, 87)) +>T : Symbol(T, Decl(narrowingNoInfer2.ts, 10, 14)) + + u: U2; +>u : Symbol(Box.u, Decl(narrowingNoInfer2.ts, 10, 18)) +>U2 : Symbol(U2, Decl(narrowingNoInfer2.ts, 6, 1)) + + t: T; +>t : Symbol(Box.t, Decl(narrowingNoInfer2.ts, 11, 10)) +>T : Symbol(T, Decl(narrowingNoInfer2.ts, 10, 14)) +} + +declare const box: Box; +>box : Symbol(box, Decl(narrowingNoInfer2.ts, 15, 13)) +>Box : Symbol(Box, Decl(narrowingNoInfer2.ts, 8, 87)) + +box.u; +>box.u : Symbol(Box.u, Decl(narrowingNoInfer2.ts, 10, 18)) +>box : Symbol(box, Decl(narrowingNoInfer2.ts, 15, 13)) +>u : Symbol(Box.u, Decl(narrowingNoInfer2.ts, 10, 18)) + +function f2(u: U2) { +>f2 : Symbol(f2, Decl(narrowingNoInfer2.ts, 16, 6)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 18, 12)) +>U2 : Symbol(U2, Decl(narrowingNoInfer2.ts, 6, 1)) + + if (u.kind === "a") { +>u.kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 8, 19), Decl(narrowingNoInfer2.ts, 8, 46), Decl(narrowingNoInfer2.ts, 8, 74)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 18, 12)) +>kind : Symbol(kind, Decl(narrowingNoInfer2.ts, 8, 19), Decl(narrowingNoInfer2.ts, 8, 46), Decl(narrowingNoInfer2.ts, 8, 74)) + + u.a; +>u.a : Symbol(a, Decl(narrowingNoInfer2.ts, 8, 30)) +>u : Symbol(u, Decl(narrowingNoInfer2.ts, 18, 12)) +>a : Symbol(a, Decl(narrowingNoInfer2.ts, 8, 30)) + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.types b/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.types new file mode 100644 index 0000000000000..7c6857340b3fd --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/narrowingNoInfer2.types @@ -0,0 +1,71 @@ +//// [tests/cases/compiler/narrowingNoInfer2.ts] //// + +=== narrowingNoInfer2.ts === +type U1 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; +>U1 : U1 +>kind : "a" +>a : number +>kind : "b" +>b : number +>kind : "c" + +function f1(u: U1) { +>f1 : (u: U1) => void +>u : U1 + + if (u.kind === "a") { +>u.kind === "a" : boolean +>u.kind : "a" | "b" | "c" +>u : { kind: "a"; a: number; } | { kind: "b"; b: number; } | { kind: "c"; } +>kind : "a" | "b" | "c" +>"a" : "a" + + u.a; +>u.a : number +>u : { kind: "a"; a: number; } +>a : number + } +} + +type U2 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; +>U2 : U2 +>kind : "a" +>a : number +>kind : "b" +>b : number +>kind : "c" + +interface Box { + u: U2; +>u : U2 + + t: T; +>t : T +} + +declare const box: Box; +>box : Box + +box.u; +>box.u : U2 +>box : Box +>u : U2 + +function f2(u: U2) { +>f2 : (u: U2) => void +>u : U2 + + if (u.kind === "a") { +>u.kind === "a" : boolean +>u.kind : "a" | "b" | "c" +>u : { kind: "a"; a: number; } | { kind: "b"; b: number; } | { kind: "c"; } +>kind : "a" | "b" | "c" +>"a" : "a" + + u.a; +>u.a : number +>u : { kind: "a"; a: number; } +>a : number + } +} + diff --git a/tsc/testdata/tests/cases/compiler/narrowingNoInfer2.ts b/tsc/testdata/tests/cases/compiler/narrowingNoInfer2.ts new file mode 100644 index 0000000000000..aee756782f0e2 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/narrowingNoInfer2.ts @@ -0,0 +1,26 @@ +// @strict: true +// @noEmit: true + +type U1 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; + +function f1(u: U1) { + if (u.kind === "a") { + u.a; + } +} + +type U2 = NoInfer<{ kind: "a"; a: number } | { kind: "b"; b: number }> | { kind: "c" }; + +interface Box { + u: U2; + t: T; +} + +declare const box: Box; +box.u; + +function f2(u: U2) { + if (u.kind === "a") { + u.a; + } +}