From cdda5dfd61fd0b3a7288619895ec3f85ebec1c70 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 23 Nov 2016 18:22:57 -0800 Subject: [PATCH 1/2] keyof T is a literal contextual type --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6d5a8b74cb3bd..f0c6e4edb42a8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14870,7 +14870,7 @@ namespace ts { } contextualType = apparentType; } - return maybeTypeOfKind(contextualType, TypeFlags.Literal); + return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index)); } return false; } From 2cec4c5ebb2cfde2119f04b299fc91fdf05f183c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 23 Nov 2016 18:28:59 -0800 Subject: [PATCH 2/2] Add regression test --- .../keyofIsLiteralContexualType.errors.txt | 32 +++++++++++++++++++ .../reference/keyofIsLiteralContexualType.js | 23 +++++++++++++ .../compiler/keyofIsLiteralContexualType.ts | 13 ++++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/baselines/reference/keyofIsLiteralContexualType.errors.txt create mode 100644 tests/baselines/reference/keyofIsLiteralContexualType.js create mode 100644 tests/cases/compiler/keyofIsLiteralContexualType.ts diff --git a/tests/baselines/reference/keyofIsLiteralContexualType.errors.txt b/tests/baselines/reference/keyofIsLiteralContexualType.errors.txt new file mode 100644 index 0000000000000..8424d528b2292 --- /dev/null +++ b/tests/baselines/reference/keyofIsLiteralContexualType.errors.txt @@ -0,0 +1,32 @@ +tests/cases/compiler/keyofIsLiteralContexualType.ts(5,9): error TS2322: Type '("a" | "b" | "c")[]' is not assignable to type 'keyof T[]'. + Type '"a" | "b" | "c"' is not assignable to type 'keyof T'. + Type '"a" | "b" | "c"' is not assignable to type '"a" | "b"'. + Type '"c"' is not assignable to type '"a" | "b"'. + Type '"c"' is not assignable to type 'keyof T'. + Type '"c"' is not assignable to type '"a" | "b"'. +tests/cases/compiler/keyofIsLiteralContexualType.ts(13,11): error TS2339: Property 'b' does not exist on type 'Pick<{ a: number; b: number; c: number; }, "a" | "c">'. + + +==== tests/cases/compiler/keyofIsLiteralContexualType.ts (2 errors) ==== + // keyof T is a literal contextual type + + function foo() { + let a: (keyof T)[] = ["a", "b"]; + let b: (keyof T)[] = ["a", "b", "c"]; + ~ +!!! error TS2322: Type '("a" | "b" | "c")[]' is not assignable to type 'keyof T[]'. +!!! error TS2322: Type '"a" | "b" | "c"' is not assignable to type 'keyof T'. +!!! error TS2322: Type '"a" | "b" | "c"' is not assignable to type '"a" | "b"'. +!!! error TS2322: Type '"c"' is not assignable to type '"a" | "b"'. +!!! error TS2322: Type '"c"' is not assignable to type 'keyof T'. +!!! error TS2322: Type '"c"' is not assignable to type '"a" | "b"'. + } + + // Repro from #12455 + + declare function pick(obj: T, propNames: K[]): Pick; + + let x = pick({ a: 10, b: 20, c: 30 }, ["a", "c"]); + let b = x.b; // Error + ~ +!!! error TS2339: Property 'b' does not exist on type 'Pick<{ a: number; b: number; c: number; }, "a" | "c">'. \ No newline at end of file diff --git a/tests/baselines/reference/keyofIsLiteralContexualType.js b/tests/baselines/reference/keyofIsLiteralContexualType.js new file mode 100644 index 0000000000000..5325b97617483 --- /dev/null +++ b/tests/baselines/reference/keyofIsLiteralContexualType.js @@ -0,0 +1,23 @@ +//// [keyofIsLiteralContexualType.ts] +// keyof T is a literal contextual type + +function foo() { + let a: (keyof T)[] = ["a", "b"]; + let b: (keyof T)[] = ["a", "b", "c"]; +} + +// Repro from #12455 + +declare function pick(obj: T, propNames: K[]): Pick; + +let x = pick({ a: 10, b: 20, c: 30 }, ["a", "c"]); +let b = x.b; // Error + +//// [keyofIsLiteralContexualType.js] +// keyof T is a literal contextual type +function foo() { + var a = ["a", "b"]; + var b = ["a", "b", "c"]; +} +var x = pick({ a: 10, b: 20, c: 30 }, ["a", "c"]); +var b = x.b; // Error diff --git a/tests/cases/compiler/keyofIsLiteralContexualType.ts b/tests/cases/compiler/keyofIsLiteralContexualType.ts new file mode 100644 index 0000000000000..dd14de9228947 --- /dev/null +++ b/tests/cases/compiler/keyofIsLiteralContexualType.ts @@ -0,0 +1,13 @@ +// keyof T is a literal contextual type + +function foo() { + let a: (keyof T)[] = ["a", "b"]; + let b: (keyof T)[] = ["a", "b", "c"]; +} + +// Repro from #12455 + +declare function pick(obj: T, propNames: K[]): Pick; + +let x = pick({ a: 10, b: 20, c: 30 }, ["a", "c"]); +let b = x.b; // Error \ No newline at end of file