I have a function that takes one parameter of type: Options
. Type Options
contains a couple of properties (obj1
and obj2
) that are basically plain old js objects and an item
property whose value should be a key of obj1
or a key of obj2
. Is it possible to achieve this in typescript? Here’s what I tried:
declare function f<t1 extends string = never, t2 extends string = never>(opts?: Options<t1, t2>): void;
export interface Options<t1 extends string = never, t2 extends string = never> {
obj1: {[K in t1]: any}
obj2: {[K in t2]: any}
item: t1 | t2;
}
// this should work but doesn't
f({
obj1: {
red: null,
green: null
},
obj2: {
black: null,
white: null
},
item: 'white'
});
Basically I was hoping that type argument t1
would resolve to red | green
and t2
to black | white
so that t1 | t2
would resolve to red | green | black | white
. Instead what happens seems to be that t1 | t2
is first resolved to white
, then both t1
and t2
are inferred to be white
as well. Any hint?