I'm attempting to extract an object's values and use them as keys for the interface. Here is my approach:
const obj = {
a: 'foo',
b: 'bar',
} as const;
type A<T extends object, K extends keyof T = keyof T> = {
[x: string]: T[K];
}[number];
// I encounter an error with this one:
// Type 'T[string]' is not assignable to type 'symbol'
type B<T extends object> = {
[K in A<T>]: any;
};
// Even when trying variations like this, it still produces the same error.
type C<T extends typeof obj = typeof obj> = {
[K in A<T>]: any;
};
// However, this alternative works without issues.
type D = {
[K in A<typeof obj>]: any;
};
// Successfully defining this one.
const a: D = {
foo: 1,
bar: 1,
};
This problem has me stumped, can someone help explain why this isn't working? (or if there are better alternatives)?