Issue
I want to create a type in TypeScript and use values of another object for its keys
const routes = {
home: "HOME",
profile: "PROFILE"
}
export type NavigatorParamList = {
[routes.home]: undefined;
[routes.profile]: undefined;
};
I know that this code is not working! Any idea how to do it?
Solution
Your object's properties are variable strings and TypeScript won't let you use them as computed properties for a type unless you make them readonly literals with as const
.
const routes = {
home: "HOME",
profile: "PROFILE"
} as const;
Answered By - skara9
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.