react-navigation / react-navigation/react-navigation.github.io

typescript - type checking route params when some params are required while other are optional

オープン
#1,212 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
324
フォーク
2k
PR マージ指標
30日以内にマージされた PR はありません

説明

regarding: https://reactnavigation.org/docs/typescript/#type-checking-the-navigator

it says:

Specifying undefined means that the route doesn't have params. A union type with undefined (e.g. SomeType | undefined) means that params are optional.

here it says SomeType | undefined, where it looks like its referring to a single param. but "params" is plural and seems to refer to all the route params.

however, in the above example it shows:

type RootStackParamList = {
  Home: undefined;
  Profile: { userId: string };
  Feed: { sort: 'latest' | 'top' } | undefined;
};

this example shows that either a route takes no params, takes 1 required param or 1 optional param. but what if a route takes both required and optional params? in this case i cannot type the entire param object to be undefined

so something like this:

type RootStackParamList = {
  SomeRoute: {
   param1: string,
   param2: string | undefined,
  }
};

i tried this, however it still shows errors when not passing the optional params. so this code:

navigation.navigate('SomeRoute', {
 param1: 'some string'
});

shows this error:

Argument of type '{ param1: string; }' is not assignable to parameter of type '{ param2: string | undefined; param1: string; }'.
Property 'param2' is missing in type '{ param1: string; }' but required in type '{ param2: string | undefined; param1: string; }'.ts(2345)

in my case, im setting param2 in SomeRoute with useLayoutEffect with setParams(), so i need to type it or else that shows in error.

Type '{ param2: any; }' is not assignable to type 'Partial<Readonly<{ param1: string; }>>'.
Object literal may only specify known properties, and 'param2' does not exist in type 'Partial<Readonly<{ param1: string; }>>'.ts(2322)

this error disappears when typing it like i did. but i still get the other error.

i have also tried setting the types as optional with a question mark. but this also shows an error if the screen takes no other params.

so this no longer shows an error:

type RootStackParamList = {
  SomeRoute: {
   param1: string;
   param2?: string | undefined,
  }
};
navigation.navigate('SomeRoute', {
 param1: 'some string,
});

but this does:

type RootStackParamList = {
  SomeRoute: {
   param2?: string | undefined,
  }
};
navigation.navigate('SomeRoute');

is it possible to define route params like this? where some are required while others are optional, not just the while params object?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue でリンクされている TypeScript のナビゲーターの型チェックページを確認し、RootStackParamList の例と報告者が説明している setParams の動作も確認します。必須パラメーターとオプションパラメーターを持つルートをどのように表現するかを明確にし、ドキュメントに記載されているナビゲーション呼び出しがサポートされている型と一致するよう、該当する例または説明を更新します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
react-native, typescript
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。