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

AppLoadContext type augmentation requires undefined check #3988

Closed
justinwaite opened this issue Aug 12, 2022 · 1 comment
Closed

AppLoadContext type augmentation requires undefined check #3988

justinwaite opened this issue Aug 12, 2022 · 1 comment

Comments

@justinwaite
Copy link
Contributor

What version of Remix are you using?

1.6.8

Steps to Reproduce

Augment the server-runtime module and declare properties on the AppLoadContext interface

declare module "@remix-run/server-runtime" {
  interface AppLoadContext {
    userId: number;
  }
}

Expected Behavior

I would expect that to be able to use context without doing any null/undefined checks, since I've written the "guarantee" that I indeed have this context in my app.

export async function loader({context}: LoaderArgs) {
  getSomeData(context.userId); // I would like to be able to do this
}

Actual Behavior

Because the type for context is AppLoadContext | undefined in the loader and action args, theres' no way that I know of to tell typescript that it will never be undefined. This means that whenever you wish to use context, you must either assert its existence with ! , or you must actually check that it exists to make typescript happy.

export async function loader({context}: LoaderArgs) {
  getSomeData(context!.userId); // but instead I have to do this
}
export async function loader({context}: LoaderArgs) {
  invariant(context); // or this
  getSomeData(context.userId);
}
@justinwaite
Copy link
Contributor Author

justinwaite commented Aug 12, 2022

I had a hard time trying to decide if this should be an issue or a discussion, so I chose issue. Let me know if I'm wrong!

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

No branches or pull requests

1 participant