@@ -16,6 +16,8 @@ Things to keep in mind:
1616
1717Types in React Query generally flow through very well so that you don't have to provide type annotations for yourself
1818
19+ [ // ] : #  ' TypeInference1 ' 
20+ 
1921``` tsx 
2022const   { data } =  useQuery ({
2123  //     ^? const data: number | undefined
@@ -24,11 +26,10 @@ const { data } = useQuery({
2426})
2527``` 
2628
27- [ // ] : #  ' Playground1 ' 
28- 
2929[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFC8MQAdqnhIAJnRh0icALwoM2XHgAUAbSqDkIAEa4qAXQA0cFQEo5APjgAFciGAYAdLVQQANgDd0KgKxmzXgB6ILgw8IA9AH5eIA ) 
3030
31- [ // ] : #  ' Playground1 ' 
31+ [ // ] : #  ' TypeInference1 ' 
32+ [ // ] : #  ' TypeInference2 ' 
3233
3334``` tsx 
3435const   { data } =  useQuery ({
@@ -39,14 +40,14 @@ const { data } = useQuery({
3940})
4041``` 
4142
42- [ // ] : #  ' Playground2 ' 
43- 
4443[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFC8MQAdqnhIAJnRh0icALwoM2XHgAUAbSox0IqgF0ANHBUBKOQD44ABXIhgGAHS1UEADYA3dCoCsxw0gwu6EwAXHASUuZhknT2MBAAyjBQwIIA5iaExrwA9Nlw+QUAegD8vEA ) 
4544
46- [ // ] : #  ' Playground2 ' 
45+ [ // ] : #  ' TypeInference2 ' 
4746
4847This works best if your ` queryFn `  has a well-defined returned type. Keep in mind that most data fetching libraries return ` any `  per default, so make sure to extract it to a properly typed function:
4948
49+ [ // ] : #  ' TypeInference3 ' 
50+ 
5051``` tsx 
5152const   fetchGroups =  ():  Promise <Group []> => 
5253  axios .get (' /groups'  ).then ((response ) =>  response .data )
@@ -55,16 +56,16 @@ const { data } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups })
5556//       ^? const data: Group[] | undefined
5657``` 
5758
58- [ // ] : #  ' Playground3 ' 
59- 
6059[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFCiSw4dAB7AIqUuUpURY1Nx68YeMOjgBxcsjBwAvIjjAAJgC44AO2QgARriK9eDCOdTwS6GAwAWmiNon6ABQAlGYAClLAGAA8vtoA2gC6AHx6qbLiAHQA5h6BVAD02Vpg8sGZMF7o5oG0qJAuarqpdQ0YmUZ0MHTBDjxOLvBInd1EeigY2Lh4gfFUxX6lVIkANKQe3nGlvTwFBXAHhwB6APxwA65wI3RmW0lwAD4o5kboJMDm6Ea8QA ) 
6160
62- [ // ] : #  ' Playground3 ' 
61+ [ // ] : #  ' TypeInference3 ' 
6362
6463## Type Narrowing  
6564
6665React Query uses a [ discriminated union type] ( https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions )  for the query result, discriminated by the ` status `  field and the derived status boolean flags. This will allow you to check for e.g. ` success `  status to make ` data `  defined:
6766
67+ [ // ] : #  ' TypeNarrowing ' 
68+ 
6869``` tsx 
6970const   { data, isSuccess } =  useQuery ({
7071  queryKey: [' test'  ],
@@ -77,16 +78,16 @@ if (isSuccess) {
7778}
7879``` 
7980
80- [ // ] : #  ' Playground4 ' 
81- 
8281[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFC8MQAdqnhIAJnRh0ANHGCoAysgYN0qVETgBeFBmy48ACgDaVGGphUAurMMBKbQD44ABXIh56AHS1UEADYAbuiGAKx2dry8wCRwhvJKKmqoDgi8cBlwElK8APS5GQB6APy8hLxAA ) 
8382
84- [ // ] : #  ' Playground4 ' 
83+ [ // ] : #  ' TypeNarrowing ' 
8584
8685## Typing the error field  
8786
8887The type for error defaults to ` unknown ` . This is in line with what TypeScript gives you per default in a catch clauses (see [ useUnknownInCatchVariables] ( https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#use-unknown-catch-variables ) ). The safest way to work with ` error `  would be to perform a runtime check; another way would be to explicitly define types for ` data `  and ` error ` :
8988
89+ [ // ] : #  ' TypingError ' 
90+ 
9091``` tsx 
9192const   { error } =  useQuery ({ queryKey: [' groups'  ], queryFn: fetchGroups  })
9293//       ^? const error: unknown
@@ -97,22 +98,19 @@ if (error instanceof Error) {
9798}
9899``` 
99100
100- [ // ] : #  ' Playground5 ' 
101- 
102101[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFCiSw4dAB7AIqUuUpURY1Nx68YeMOjgBxcsjBwAvIjjAAJgC44AO2QgARriK9eDCOdTwS6GAwAWmiNon6ABQAlGYAClLAGAA8vtoA2gC6AHx6qbLiAHQA5h6BVAD02Vpg8sGZMF7o5oG0qJAuarqpdQ0YmUZ0MHTBDjxOLvBIuORQRHooGNi4eIHxVMV+pVSJADSkHt5xpb08BQVwh0cAegD8fcAkcIEj0IaDdOYM6BBXAKJQo8GIvIe3ULx9nAzrxCEA ) 
103102
104- [ // ] : #  ' Playground5 ' 
103+ [ // ] : #  ' TypingError ' 
104+ [ // ] : #  ' TypingError2 ' 
105105
106106``` tsx 
107107const   { error } =  useQuery <Group [], Error >([' groups'  ], fetchGroups )
108108//       ^? const error: Error | null
109109``` 
110110
111- [ // ] : #  ' Playground6 ' 
112- 
113111[ typescript playground] ( https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAORToCGAxjALQCOO+VAsAFCiSw4dAB7AIqUuUpURY1Nx68YeMOjgBxcsjBwAvIjjAAJgC44AO2QgARriK9eDCOdTwS6GAwAWmiNon6ABQAlGYAClLAGAA8vtoA2gC6AHx6qbLiAHQA5h6BVAD02Vpg8sGZMF7o5oG0qJAuarqpdQ0YmUZ0MHTBDjxOLvBIuORQRHooGNi4eLElSQA0cACiUKPJgfFUxX6lVIlL7p4+Jai9PAUFcNc3AHoA-LxAA ) 
114112
115- [ // ] : #  ' Playground6 ' 
113+ [ // ] : #  ' TypingError2 ' 
116114[ // ] : #  ' Materials ' 
117115
118116## Further Reading  
0 commit comments