I have a type like this:
type Wrapper<T> = {
[K in keyof T]: {
value: T[K];
meta: string[];
}
}
I have a function createWrapper that takes an object and creates a wrapper out of it:
const createWrapper = <T extends object>(obj: T): Wrapper<T> => {
let wrapper: any = {};
for (let key in obj) {
wrapper[key] = {
value: obj[key],
meta: []
};
}
return wrapper as Wrapper<T>;
}
How could I type the object creation inside createWrapper to avoid any and as Wrapper<T> types?
Thanks a lot for the help!
let wrapper: Partial<Wrapper<T>> = {}instead of usingany. But after the loop the compiler will not understand by itself that all the keys are present. You can make user-defined type guards, but a well-placed assertion is probably your best bet here.