When dealing with this scenario, What should the test() function's generic be?
interface A {
type: 'a' ;
state: number;
}
interface B {
type :'b',
state: string;
}
type OneOfThem = A | B;
function test<T extends OneOfThem>(type: T['type'], state: T["state"]): T {
return {type, state} as T
}
test('a', 3) // it should not generate an error
test('a', "asd") // it should result in an error
In this instance, the test function's types are operating like
function test<OneOfThem>(type: "a" | "b", state: string | number): OneOfThem
therefore, neither of these expressions throw errors.