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 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

檢視 issue 中連結的 TypeScript 導覽器型別檢查頁面,包括其中的 RootStackParamList 範例,以及回報者描述的 setParams 行為。釐清如何表示具有必要參數和選用參數的路由,並更新相關範例或說明,讓文件中的導覽呼叫符合支援的型別。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
react-native, typescript
領域
documentation
Issue 類型
文件
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。