I've gotten this far: which seems to work
function test<types extends Record<string,any>>(dict: dictionary<types>){}
type dictionary<types extends Record<string, any>> = {
[key in keyof types]: {
bar?: types[key];
foo?: (value:types[key])=>true;
};
};
test({
key1:{
bar: 2,
foo: (input:number)=>true,
},
key2:{
bar: 'hello'
foo: (input: number)=>true, // Error! "input" needs to be string
}
})
BUT!
I also need a generic type reference to the dict
parameter. And for some reason, this doesn't work
function test2<
types extends Record<string,any>,
dictionary extends dictionary2<types> // <-- Added a generic type
>(dict: dictionary){}
// Same as above
type dictionary2<types extends Record<string, any>> = {
[key in keyof types]: {
bar?: types[key];
foo?: (value:types[key])=>true;
};
};
// Same as above
test2({
key1:{
bar: 2,
foo: (input: number)=>true,
},
key2:{
bar: 'hello',
foo: (input:number)=>true,// Should be an Error (but isn't)! "input" needs to be string
}