-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Incorrect types for loaders that return optional properties #3794
Comments
As JSON doesn't support |
That's technically true, but I think |
the current type behavior encourages runtime errors (by telling Typescript that a key is definitely there when in fact it might not be). My proposed change might not be perfect, but it's miles better than that. If someone else proposes a better fix I am far from attached to my solution, but it is a solution (and a simple one at that) |
@tshddx not sure what you mean, if data.example // error because `example` key might not exist
if ("example" in data) {
// here you can access data.example
} I wouldn't know how to submit that PR in Remix, though, so I'll be quiet. 😄 |
TS handles |
This part of TypeScript is tricky, yes, I'm still not entirely used to it, however
No type errors! For optional properties both My conclusion: Having the type return from |
Fixed by #3766 |
What version of Remix are you using?
v1.6.5
Steps to Reproduce
Simple reproduction in the Typescript playground
useLoaderData<typeof loader>
undefined
is of typestring
rather thanstring | undefined
Expected Behavior
optional properties should be considered potentially undefined when returned from
useLoaderData
- even thoughJSON.parse
will never include anundefined
value, it's possible that expected keys will simply be missing which (in JS) is almost equivalent to being actually undefined).Actual Behavior
any
undefined
s or optional properties are stripped away entirely.The text was updated successfully, but these errors were encountered: