Reputation: 43
I'm trying to generate a type definition for a function that generates an object from an array of props, but I'm having problems with this.
The problem is that in { [K in T]: boolean }
, I'm not being able to iterate over the type T.
Here's what I'm trying to do:
const createObject = <T extends string[]>(props: T): { [K in T]: boolean } => {
return props.reduce((acc: any, prop: string) => {
acc[prop] = true
return acc
}, {})
}
Thanks
Upvotes: 0
Views: 64
Reputation: 2878
just a little bit workaround... here you are
const createObject = <T extends string>(props: T[]): {[K in T]: boolean} => {
return props.reduce((acc: any, prop: string) => {
acc[prop] = true;
return acc;
}, {});
};
const testObj = createObject(['one', 'two']);
const one = testObj.one;
const two = testObj.two;
const three = testObj.three; // ERROR
Upvotes: 3