I’m working with a complex TypeScript type and trying to manage it within a function. Here’s what I have:
type T = { a: number } | { b: number }
function func(k: 'a' | 'b', v: number) {
// error message below
const t: T = { [k]: v }
console.log(t);
}
However, I encountered an error:
Type '{ [x: string]: number; }' is not assignable to type 'T'.
Property 'b' is missing in type '{ [x: string]: number; }' but required in type '{ b: number; }'.
Any ideas on how to resolve this issue?