1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
// @strict: true
// @declaration: true
// Repro from #49242
type Types = {
first: { a1: true };
second: { a2: true };
third: { a3: true };
}
class Test {
entries: { [T in keyof Types]?: Types[T][] };
constructor() {
this.entries = {};
}
addEntry<T extends keyof Types>(name: T, entry: Types[T]) {
if (!this.entries[name]) {
this.entries[name] = [];
}
this.entries[name]?.push(entry);
}
}
// Repro from #49338
type TypesMap = {
[0]: { foo: 'bar'; };
[1]: { a: 'b'; };
};
type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];
type TypeHandlers = {
[T in keyof TypesMap]?: (p: P<T>) => void;
};
const typeHandlers: TypeHandlers = {
[0]: (p) => console.log(p.foo),
[1]: (p) => console.log(p.a),
};
const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
typeHandlers[p.t]?.(p);
|