Add regression test

This commit is contained in:
Anders Hejlsberg 2016-11-23 18:28:59 -08:00
parent cdda5dfd61
commit 2cec4c5ebb
3 changed files with 68 additions and 0 deletions

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