Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to add a placeholder for this type?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading