For example, type TCmd is defined like below
type TCmd = "hello" | "world";
export function foo(cmd: TCmd) {
let data: Record<TCmd, boolean> = {
hello: cmd === "hello",
world: cmd === "world",
};
return data;
}
Is the inline type generic able to do the same as the one below (incorrect example)? The purpose is to minimize the use of many simple external type definitions, e.g. like "hello"|"world"
export function bar<T extends "hello"|"world">(cmd: T) {
let data: Record<T, boolean> = {
hello: cmd === "hello", // error
world: cmd === "world", // error
};
return data;
}
The error I get is this:
Type '{ hello: boolean; world: boolean; }' is not assignable to type 'Record<T, boolean>'. Object literal may only specify known properties, and 'hello' does not exist in type 'Record<T, boolean>'.ts(2322)