Skip to content
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

Type narrowing not working properly in single-fetch / plain object returns from loaders #12854

Open
AndreSilva1993 opened this issue Jan 24, 2025 · 0 comments
Labels

Comments

@AndreSilva1993
Copy link

AndreSilva1993 commented Jan 24, 2025

I'm using React Router as a...

framework

Reproduction

Previously when using the json() method we were able to type narrow the loader data properly.

Image

If we just return plain objects, TypeScript is still marking the property as undefined even though it never is. It also contains the other property as possibly that should never be present given the type narrowing.

Image

Tested this with @remix-run/node / @remix-run/react and react-router v7 and the issue is present in both.

https://github.com/AndreSilva1993/single-fetch-type-narrowing -> React Router v7
https://github.com/AndreSilva1993/json-vs-single-fetch-type-narrowing -> remix-run/node and remix-run/react with v3_singleFetch enabled.

System Info

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 87.89 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    Yarn: 4.6.0 - ~/.nvm/versions/node/v22.11.0/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
  Browsers:
    Chrome: 131.0.6778.267
    Safari: 18.2
  npmPackages:
    @react-router/dev: ^7.1.3 => 7.1.3
    @react-router/node: ^7.1.3 => 7.1.3
    @react-router/serve: ^7.1.3 => 7.1.3
    react-router: ^7.1.3 => 7.1.3
    vite: ^5.4.11 => 5.4.14

Used Package Manager

npm

Expected Behavior

I would expect the type narrowing to work as expected.

Actual Behavior

The returned object ends up being an union of all possible properties stating that they could be undefined rendering type narrowing unusable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant