Issue
I am trying to create a TS validated utility that iterates over the first level properties of the object and returns a new one with its property name.
JS-wise it's quite straightforward, but I would appreciate a bit if you can help me a bit define a correct type structure. I've created a TS Playground link with the code example
P.S: Feel free to drop any suggestion on type refactor if you know a better way :-)
Solution
This is absolutely possible and simple with a mapped type:
type PropMap<T> = {
[K in keyof T]: K;
};
And then we change the definition of the function to return this:
function createObjectKeys<T> (object: T): PropMap<T> {
Unfortunately a cast is required here:
return keys as PropMap<T>;
But if you try it in the playground below you'll see that it produces the correct types:
Answered By - halfdecent
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.