diff --git a/tests/baselines/reference/nonNullableReduction.js b/tests/baselines/reference/nonNullableReduction.js new file mode 100644 index 00000000000..57de1f96a7b --- /dev/null +++ b/tests/baselines/reference/nonNullableReduction.js @@ -0,0 +1,33 @@ +//// [nonNullableReduction.ts] +// Repros from #43425 + +type Transform1 = ((value: string) => T) | (string extends T ? undefined : never); +type Transform2 = string extends T ? ((value: string) => T) | undefined : (value: string) => T; + +function test(f1: Transform1, f2: Transform2) { + f1?.("hello"); + f2?.("hello"); +} + +function f1(x: T | (string extends T ? null | undefined : never)) { + let z = x!; // NonNullable +} + +function f2(x: T | U) { + let z = x!; // NonNullable +} + + +//// [nonNullableReduction.js] +"use strict"; +// Repros from #43425 +function test(f1, f2) { + f1 === null || f1 === void 0 ? void 0 : f1("hello"); + f2 === null || f2 === void 0 ? void 0 : f2("hello"); +} +function f1(x) { + var z = x; // NonNullable +} +function f2(x) { + var z = x; // NonNullable +} diff --git a/tests/baselines/reference/nonNullableReduction.symbols b/tests/baselines/reference/nonNullableReduction.symbols new file mode 100644 index 00000000000..6007449ca6d --- /dev/null +++ b/tests/baselines/reference/nonNullableReduction.symbols @@ -0,0 +1,61 @@ +=== tests/cases/compiler/nonNullableReduction.ts === +// Repros from #43425 + +type Transform1 = ((value: string) => T) | (string extends T ? undefined : never); +>Transform1 : Symbol(Transform1, Decl(nonNullableReduction.ts, 0, 0)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 2, 16)) +>value : Symbol(value, Decl(nonNullableReduction.ts, 2, 23)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 2, 16)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 2, 16)) + +type Transform2 = string extends T ? ((value: string) => T) | undefined : (value: string) => T; +>Transform2 : Symbol(Transform2, Decl(nonNullableReduction.ts, 2, 85)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 3, 16)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 3, 16)) +>value : Symbol(value, Decl(nonNullableReduction.ts, 3, 42)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 3, 16)) +>value : Symbol(value, Decl(nonNullableReduction.ts, 3, 78)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 3, 16)) + +function test(f1: Transform1, f2: Transform2) { +>test : Symbol(test, Decl(nonNullableReduction.ts, 3, 98)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 5, 14)) +>f1 : Symbol(f1, Decl(nonNullableReduction.ts, 5, 17)) +>Transform1 : Symbol(Transform1, Decl(nonNullableReduction.ts, 0, 0)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 5, 14)) +>f2 : Symbol(f2, Decl(nonNullableReduction.ts, 5, 35)) +>Transform2 : Symbol(Transform2, Decl(nonNullableReduction.ts, 2, 85)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 5, 14)) + + f1?.("hello"); +>f1 : Symbol(f1, Decl(nonNullableReduction.ts, 5, 17)) + + f2?.("hello"); +>f2 : Symbol(f2, Decl(nonNullableReduction.ts, 5, 35)) +} + +function f1(x: T | (string extends T ? null | undefined : never)) { +>f1 : Symbol(f1, Decl(nonNullableReduction.ts, 8, 1)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 10, 12)) +>x : Symbol(x, Decl(nonNullableReduction.ts, 10, 15)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 10, 12)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 10, 12)) + + let z = x!; // NonNullable +>z : Symbol(z, Decl(nonNullableReduction.ts, 11, 7)) +>x : Symbol(x, Decl(nonNullableReduction.ts, 10, 15)) +} + +function f2(x: T | U) { +>f2 : Symbol(f2, Decl(nonNullableReduction.ts, 12, 1)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 14, 12)) +>U : Symbol(U, Decl(nonNullableReduction.ts, 14, 14)) +>x : Symbol(x, Decl(nonNullableReduction.ts, 14, 43)) +>T : Symbol(T, Decl(nonNullableReduction.ts, 14, 12)) +>U : Symbol(U, Decl(nonNullableReduction.ts, 14, 14)) + + let z = x!; // NonNullable +>z : Symbol(z, Decl(nonNullableReduction.ts, 15, 7)) +>x : Symbol(x, Decl(nonNullableReduction.ts, 14, 43)) +} + diff --git a/tests/baselines/reference/nonNullableReduction.types b/tests/baselines/reference/nonNullableReduction.types new file mode 100644 index 00000000000..5c0863fc379 --- /dev/null +++ b/tests/baselines/reference/nonNullableReduction.types @@ -0,0 +1,50 @@ +=== tests/cases/compiler/nonNullableReduction.ts === +// Repros from #43425 + +type Transform1 = ((value: string) => T) | (string extends T ? undefined : never); +>Transform1 : Transform1 +>value : string + +type Transform2 = string extends T ? ((value: string) => T) | undefined : (value: string) => T; +>Transform2 : Transform2 +>value : string +>value : string + +function test(f1: Transform1, f2: Transform2) { +>test : (f1: Transform1, f2: Transform2) => void +>f1 : Transform1 +>f2 : Transform2 + + f1?.("hello"); +>f1?.("hello") : T | undefined +>f1 : Transform1 +>"hello" : "hello" + + f2?.("hello"); +>f2?.("hello") : T | undefined +>f2 : ((value: string) => T) | ((value: string) => T) | undefined +>"hello" : "hello" +} + +function f1(x: T | (string extends T ? null | undefined : never)) { +>f1 : (x: T | (string extends T ? null | undefined : never)) => void +>x : T | (string extends T ? null | undefined : never) +>null : null + + let z = x!; // NonNullable +>z : NonNullable +>x! : NonNullable +>x : T | (string extends T ? null | undefined : never) +} + +function f2(x: T | U) { +>f2 : (x: T | U) => void +>null : null +>x : T | U + + let z = x!; // NonNullable +>z : NonNullable +>x! : NonNullable +>x : T | U +} + diff --git a/tests/cases/compiler/nonNullableReduction.ts b/tests/cases/compiler/nonNullableReduction.ts new file mode 100644 index 00000000000..1465d0e650a --- /dev/null +++ b/tests/cases/compiler/nonNullableReduction.ts @@ -0,0 +1,19 @@ +// @strict: true + +// Repros from #43425 + +type Transform1 = ((value: string) => T) | (string extends T ? undefined : never); +type Transform2 = string extends T ? ((value: string) => T) | undefined : (value: string) => T; + +function test(f1: Transform1, f2: Transform2) { + f1?.("hello"); + f2?.("hello"); +} + +function f1(x: T | (string extends T ? null | undefined : never)) { + let z = x!; // NonNullable +} + +function f2(x: T | U) { + let z = x!; // NonNullable +}