#useLoaderData<typeof loader>() not giving me types from loader
1 messages · Page 1 of 1 (latest)
If you say:
const loader: LoaderFunction = (/*...*/) => {/*...*/}
Then TS sees "Oh, loader is a general LoaderFunction. That's its type. Don't bother inferring a more specific types from its implementation".
If you want type inference you have a couple options:
-
Use
satisfies LoaderFunctioninstead. that tells TS to make sureloaderis aLoaderFunctionbut to still infer a more specific type. But gettingsatisfiesto work sometimes involves some ugly()wrapping your function, so I don't often use this solution even though its pretty much exactly the semantics I'd want. -
Type the
loaderargs directly:
const loader = ({request}: LoaderFunctionArgs) => {/*...*/}
^that tells TS to just use LoaderFunctionArgs for the args, but to still infer the return type for loader
I usually just use (2) since its a smaller change
@strong quail and @lament anvil
I face same issue but in i am not use typescript i use Javascript
below is my code picture.
@true storm do const { translations } = useLoaderData<typeof loader>(); to have inference on loader data type.
@mellow hull i use Javascript and your solution is reliable with Typescript.
my error :- Property 'filterData' does not exist on type 'unknown'
That's a TS error. See ts(2339) in the error message? You probably have a tsconfig.json file with allowJs turned on
but in the end that means you are using TypeScript's type checking
Hi, I see my tsconfig.json file there is "allowJs": true. So what I do next to resolve this error.
Three options
- set allowJs to false so you stop type checking
- or use a jsdoc annotation to either type the loader data or set it to any
- or manually guard against the types with a series of conditions
Thanks for the answer.👍
Option 2 looks like this, it would be my recommendation if you want to stick to JS
#1146431537672368299 message
removing ":LoaderFunction" is the cure
not sure what its point is
It also threw me at start
It's basically a legacy type export at this point. I'm not sure why the Remix Team didn't get rid of it with the v2 release since it oft creates confusion for learning Remix.
its still useful if you use satisfies
that's nice because it also makes sure you are returning something meaningful. just too bad satisfies for functions is kinda ugly to write