From 34994627f03d35a37643ca698d47d0b507716fd3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 1 Oct 2018 16:21:35 -0700 Subject: [PATCH] Add tests --- .../types/keyof/keyofAndIndexedAccess.ts | 9 +-------- .../keyof/keyofAndIndexedAccessErrors.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts index 7748240f80d..4a69af307af 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts @@ -302,23 +302,16 @@ type S2 = { b: string; }; -function f90(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K], x4: T[K]) { +function f90(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K]) { x1 = x2; x1 = x3; - x1 = x4; x2 = x1; x2 = x3; - x2 = x4; x3 = x1; x3 = x2; - x3 = x4; - x4 = x1; - x4 = x2; - x4 = x3; x1.length; x2.length; x3.length; - x4.length; } function f91(x: T, y: T[keyof T], z: T[K]) { diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts index 3660ff418e8..a4ed608d8e7 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts @@ -122,3 +122,22 @@ function f4(k: keyof T) { k = 42; // error k = "hello"; // error } + +// Repro from #27470 + +type UndefinedKeys> = { + [K in keyof T]: undefined extends T[K] ? K : never +}; + +type MyType = {a: string, b: string | undefined} + +type Result1 = UndefinedKeys; + +const a1: Result1['a'] = 'a'; // Error +const b1: Result1['b'] = 'b'; + +function test1, K extends keyof T>(t: T, k: K) { + t[k] = 42; // Error + t[k] = "hello"; // Error + t[k] = [10, 20]; // Error +}