Merge pull request #12503 from Microsoft/pick-keyofIsLiteralContext-2.1

Pick into release-2.1: Make 'keyof T' a literal contextual type
This commit is contained in:
Daniel Rosenwasser
2016-11-27 14:32:01 -08:00
committed by GitHub
4 changed files with 69 additions and 1 deletions

View File

@@ -14870,7 +14870,7 @@ namespace ts {
}
contextualType = apparentType;
}
return maybeTypeOfKind(contextualType, TypeFlags.Literal);
return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index));
}
return false;
}

View File

@@ -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<T extends { a: string, b: string }>() {
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<T, K extends keyof T>(obj: T, propNames: K[]): Pick<T, K>;
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">'.

View File

@@ -0,0 +1,23 @@
//// [keyofIsLiteralContexualType.ts]
// keyof T is a literal contextual type
function foo<T extends { a: string, b: string }>() {
let a: (keyof T)[] = ["a", "b"];
let b: (keyof T)[] = ["a", "b", "c"];
}
// Repro from #12455
declare function pick<T, K extends keyof T>(obj: T, propNames: K[]): Pick<T, K>;
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

View File

@@ -0,0 +1,13 @@
// keyof T is a literal contextual type
function foo<T extends { a: string, b: string }>() {
let a: (keyof T)[] = ["a", "b"];
let b: (keyof T)[] = ["a", "b", "c"];
}
// Repro from #12455
declare function pick<T, K extends keyof T>(obj: T, propNames: K[]): Pick<T, K>;
let x = pick({ a: 10, b: 20, c: 30 }, ["a", "c"]);
let b = x.b; // Error