admin管理员组文章数量:1290273
I'm trying to define custom typings for a navigation library using TypeScript, and I haven't wrapped my head around creating a navigate
function that takes the Screen's name as a first argument, and the Screen's properties as a second argument while still being type safe.
I'm trying to:
- Safely type the screen name (
keyof Stack
) as a first argument - Safely encapsulate my two navigation stacks (
navigate<LoginStack>('HomeScreen')
should never work, only the screens fromLoginStack
should be possible) - Use the correct arguments for the corresponding screen name (
keyof Stack
) as the second argument
My current approach:
ScreenDefinitions.ts
:
export interface LoginStackScreens {
LoginScreen: BaseProps & { isHeadless?: boolean };
ForgotPasswordScreen: BaseProps & { email: string };
}
export interface HomeStackScreens {
HomeScreen: BaseProps & { isHeadless?: boolean };
SavedScreen: BaseProps;
ChatsListScreen: BaseProps;
MyProfileScreen: BaseProps;
PostDetailsScreen: BaseProps & {
post: Post;
};
// ... some more
}
Navigation.ts
:
type ValueOf<T> = T[keyof T];
function navigate<Stack extends HomeStackScreens | LoginStackScreens>(screenName: keyof Stack, props: ValueOf<Stack>): void {
// Navigation logic
}
navigate<HomeStackScreens>('HomeScreen', {});
While I achieved points 1. and 2., the second parameter (arguments) aren't safely typed as they contain all values from the HomeStackScreens
stack:
Whereas I only want to have BaseProps & { isHeadless?: boolean }
in this case (See HomeStackScreens
definition)
I'm trying to define custom typings for a navigation library using TypeScript, and I haven't wrapped my head around creating a navigate
function that takes the Screen's name as a first argument, and the Screen's properties as a second argument while still being type safe.
I'm trying to:
- Safely type the screen name (
keyof Stack
) as a first argument - Safely encapsulate my two navigation stacks (
navigate<LoginStack>('HomeScreen')
should never work, only the screens fromLoginStack
should be possible) - Use the correct arguments for the corresponding screen name (
keyof Stack
) as the second argument
My current approach:
ScreenDefinitions.ts
:
export interface LoginStackScreens {
LoginScreen: BaseProps & { isHeadless?: boolean };
ForgotPasswordScreen: BaseProps & { email: string };
}
export interface HomeStackScreens {
HomeScreen: BaseProps & { isHeadless?: boolean };
SavedScreen: BaseProps;
ChatsListScreen: BaseProps;
MyProfileScreen: BaseProps;
PostDetailsScreen: BaseProps & {
post: Post;
};
// ... some more
}
Navigation.ts
:
type ValueOf<T> = T[keyof T];
function navigate<Stack extends HomeStackScreens | LoginStackScreens>(screenName: keyof Stack, props: ValueOf<Stack>): void {
// Navigation logic
}
navigate<HomeStackScreens>('HomeScreen', {});
While I achieved points 1. and 2., the second parameter (arguments) aren't safely typed as they contain all values from the HomeStackScreens
stack:
Whereas I only want to have BaseProps & { isHeadless?: boolean }
in this case (See HomeStackScreens
definition)
1 Answer
Reset to default 8Try this!
function navigate<Stack extends {}>(
screenName: keyof Stack,
props: Stack[typeof screenName]
) {
// Implementation
}
Example,
type SomeStack = {
someScreen: {
test: string
}
}
navigate<SomeStack>('someScreen', {
test: 5 // Fails - should be a string!
})
本文标签: javascriptTypeScript type for value of key in objectStack Overflow
版权声明:本文标题:javascript - TypeScript type for value of key in object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499354a2381985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论