I'm trying to create an object with specific keys based on the value of the array.
Here is the code:
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 see I tried to do it using Pick, but that doesn't seem to work.
Demo: here