Skip to content

Commit b4836e3

Browse files
committed
Add tests
1 parent b876211 commit b4836e3

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @strictNullChecks: true
12
// @declaration: true
23

34
class Shape {
@@ -219,6 +220,36 @@ function f60<T>(source: T, target: T) {
219220
}
220221
}
221222

223+
function f70(func: <T, U>(k1: keyof (T | U), k2: keyof (T & U)) => void) {
224+
func<{ a: any, b: any }, { a: any, c: any }>('a', 'a');
225+
func<{ a: any, b: any }, { a: any, c: any }>('a', 'b');
226+
func<{ a: any, b: any }, { a: any, c: any }>('a', 'c');
227+
}
228+
229+
function f71(func: <T, U>(x: T, y: U) => Partial<T & U>) {
230+
let x = func({ a: 1, b: "hello" }, { c: true });
231+
x.a; // number | undefined
232+
x.b; // string | undefined
233+
x.c; // boolean | undefined
234+
}
235+
236+
function f72(func: <T, U, K extends keyof T | keyof U>(x: T, y: U, k: K) => (T & U)[K]) {
237+
let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number
238+
let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string
239+
let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean
240+
}
241+
242+
function f73(func: <T, U, K extends keyof (T & U)>(x: T, y: U, k: K) => (T & U)[K]) {
243+
let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number
244+
let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string
245+
let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean
246+
}
247+
248+
function f74(func: <T, U, K extends keyof (T | U)>(x: T, y: U, k: K) => (T | U)[K]) {
249+
let a = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'a'); // number
250+
let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean
251+
}
252+
222253
// Repros from #12011
223254

224255
class Base {
@@ -291,4 +322,36 @@ var empty = one(() => {}) // inferred as {}, expected
291322
type Handlers<T> = { [K in keyof T]: (t: T[K]) => void }
292323
declare function on<T>(handlerHash: Handlers<T>): T
293324
var hashOfEmpty1 = on({ test: () => {} }); // {}
294-
var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean }
325+
var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean }
326+
327+
// Repro from #12624
328+
329+
interface Options1<Data, Computed> {
330+
data?: Data
331+
computed?: Computed;
332+
}
333+
334+
declare class Component1<Data, Computed> {
335+
constructor(options: Options1<Data, Computed>);
336+
get<K extends keyof (Data & Computed)>(key: K): (Data & Computed)[K];
337+
}
338+
339+
let c1 = new Component1({
340+
data: {
341+
hello: ""
342+
}
343+
});
344+
345+
c1.get("hello");
346+
347+
// Repro from #12625
348+
349+
interface Options2<Data, Computed> {
350+
data?: Data
351+
computed?: Computed;
352+
}
353+
354+
declare class Component2<Data, Computed> {
355+
constructor(options: Options2<Data, Computed>);
356+
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
357+
}

tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ function f10(shape: Shape) {
6565
setProperty(shape, "name", "rectangle");
6666
setProperty(shape, "size", 10); // Error
6767
setProperty(shape, cond ? "name" : "size", 10); // Error
68+
}
69+
70+
function f20<T, U>(k1: keyof (T | U), k2: keyof (T & U), o1: T | U, o2: T & U) {
71+
o1[k1];
72+
o1[k2]; // Error
73+
o2[k1];
74+
o2[k2];
75+
o1 = o2;
76+
o2 = o1; // Error
77+
k1 = k2; // Error
78+
k2 = k1;
6879
}

0 commit comments

Comments
 (0)