I am attempting to generate an object with specific keys based on the values within an array.
Below is the code snippet:
type AllowedKeys = "foo" | "bar" | "baz"
interface Options {
keys: AllowedKeys[]
}
interface AllTypesDeclaration {
foo: string[];
baz: object[];
bar: number[];
}
function createObj(arg: Options): Pick<AllTypesDeclaration, typeof arg.keys[number]> {
return {}
}
const myObj = createObj({ keys: ['baz'] })
// This should work:
myObj.baz
// This should fail:
myObj.foo
myObj.bar
As you can observe, my attempt using Pick
does not seem to be successful.
Demo: here