Home > Enterprise >  Is there a way to add a placeholder for this type?
Is there a way to add a placeholder for this type?

Time:02-02

Obligatory: apologies if this has been asked before!

I'm working on a React Native project and would like to add a placeholder for the screen name:

type HomeNavProps = BottomTabNavigationProp<
  RootStackParamList,
  "Home"
>

Ideally, I'd like something more generic like:

type NavProps<T> = BottomTabNavigationProp<
  RootStackParamList,
  T
>

So I can then use it like:

const { navigate } = useNavigation<NavProps<"Home">>()

Granted I'm not sure how to do it, but when I try the above, I just get:

Type 'T' does not satisfy the constraint 'keyof RootStackParamList'.

For reference, RootStackParamList is:

type RootStackParamList = {
  Home: undefined
  Levels: undefined
  Map: undefined
  MyBadges: undefined
  MyImpact: undefined
}

Is something like this possible?

CodePudding user response:

You need to constrain T to the correct type. This would be the same type as its expected destination.

In this case I believe that would be: keyof RootStackParamList

type NavProps<T extends keyof RootStackParamList> = BottomTabNavigationProp<
  RootStackParamList,
  T
>
  •  Tags:  
  • Related