-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useLoaderData type inference broken when returned data has a data
key
#5211
Comments
I've this problem as well with the latest version of Remix. I just wanted to add that it also happens when using useFetcher in actions. Reproduced this in a minimal repository for you:
|
Looks like this is the problem. remix/packages/remix-server-runtime/serialize.ts Lines 21 to 31 in 006b58b
remix/packages/remix-server-runtime/responses.ts Lines 9 to 14 in 006b58b
|
Can confirm this is also a problem with actions, returning POJOs or using the json helper. |
This is still an issue with actions, typed loaders are working for me. Meaning, |
To help solve the issue, it comes from this : declare type Serialize<T> = IsAny<T> extends true ? any : T extends TypedDeferredData<infer U> ? SerializeDeferred<U> : ...
export declare type TypedDeferredData<Data extends Record<string, unknown>> = Pick<DeferredData, "init"> & {
data: Data;
}; Type narrowing tends to consider every payload with |
This comment was marked as duplicate.
This comment was marked as duplicate.
Thanks for reporting the issue! I posted it on the Discord a couple of weeks ago and was late to report it here :) For debbuging without setting up a whole environment, I would recommend the typescript sandbox. Here is a gist of the issue on TS Playground. As you can see, the type inference takes what is in the "data" property instead of considering the whole object. Another example: export async function action({ request }: ActionArgs) {
// Other returns are possible
return json({ data: { foo: "bar" }, errors: { foo: "baz" })
}
export default function Something() {
const actionData = useActionData<typeof action>()
// ^ actionData has type { foo: "bar" } 🤷♂️
const data = actionData && "data" in actionData ? actionData.data : null
// ^ ❌ data is of type string, but should be { foo: string }
// But it works if the property is named something else
// than "data", like "formData" 🤷♂️
} |
That also happened to me using Remix 1.14.0 (cloudflare runtime). |
To solve this issue, I implement |
This still occurs on Remix 1.15, and it also occurs on the I also have a typescript playground for testing. |
Closed by #5516 |
🤖 Hello there, We just published version Thanks! |
It looks like it resolved the issue for us from what I can see. |
1.16.0 is out 🎉 |
I face same issue but in my project I use JavaScript instead of TypeScript I use Remix 2.0.0 version |
@Darshan562 a bunch of types were improved in 2.1-2.4. If you still have issues after upgrading to those versions, please file a separate issue. |
What version of Remix are you using?
1.11.1
Steps to Reproduce
If the object returned from the loader includes a
data
key, the type inference returns only the items nested under that key. So in this case, typescript thinksuseLoaderData
returns just{item: number}
. The code works just fine -item
andother
are being destructured properly and displaying on the page, it's just the types that are wrong. If I rename thedata
key to something else, it works fine and typescript returns the full type, e.g.{myData: {item: number}, other: number}
. This started whendefer
was released.Expected Behavior
Type inference should work as it did before, or if
data
is a reserved name, the documentation should say as much.Actual Behavior
Typescript infers the wrong types, but the code works fine.
The text was updated successfully, but these errors were encountered: