T
TanStack2y ago
continuing-cyan

Use enabled prop for type narrowing

Simple question: Would it be feasible to use the 'enabled' prop to narrow down the type of variables used in the fetch function? Codesandbox to show the issue: https://codesandbox.io/p/sandbox/billowing-thunder-qwdy98?file=%2Fsrc%2FApp.tsx%3A2%2C37
4 Replies
extended-salmon
extended-salmon2y ago
The problem with this is that in both cases id refers to the id in the props which can be undefined. There's no connection between the enabled check and the id you pass to the queryFn. TS simply can not narrow this property.
No description
extended-salmon
extended-salmon2y ago
React Query and TypeScript
Combine two of the most powerful tools for React Apps to produce great user experience, developer experience and type safety.
continuing-cyan
continuing-cyanOP2y ago
Thnks!

Did you find this page helpful?