-
Notifications
You must be signed in to change notification settings - Fork 1k
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
mockCurrentUser
does not type-check as expected with TypeScript's strict
option
#5261
Comments
Hi @pvenable - thank you for raising another great issue. I've looked at this as part of strict mode support, but I believe the behaviour is as expected. Let's break it down: 1. Setting mockCurrentUser to null 2. Setting mockCurrentUser with a roles array By default the Unfortuantely TypeScript does not let you "override" interfaces, but simply merge them. So what happens is it merges these two things: // default Redwood interface for current user
export interface CurrentUser {
roles?: Array<string> | string
}
// and once in your project, it gets merged with (see .redwood/types/includes/all-currentUser.d.ts, in your project)
// this is the type from src/lib/auth#getCurrentUser
interface ProjectCurrentUser {
id: number,
roles: string
}
// The result of which is
interface MergedCurrentUser {
id: number,
roles: string | string | Array<string>
} I'm watching this issue over on the TypeScript repo: microsoft/TypeScript#36146 - but would be happy to hear if you have any other suggestions. 3. Importing CurrentUser type explicitly Very open to your feedback and suggestions - otherwise please do close the issue if there's nothing more to add here! |
Hi, @dac09 -- thanks for your response.
I completely agree that CurrentUser can be null. The issue I'm reporting here is that I am short on time right now so I will digest the rest of what you have written later when I have a chance, but I do still believe there is a problem when trying to pass properties other than Generating types didn't seem to address my issues either, but I will have to investigate more later. Thanks! |
After looking at this a bit more I must admit I remain stumped. I'm willing to change my For the second issue ( |
Hi Paul, Sorry for the radio silence on this - had so many competing priorities. So, I think you're right there is some strange behaviour that happens with the types - I'm stumped too. But I think I may have a solution.
So I'm going to change the generated type file like this: - import type { CurrentUser } from '@redwoodjs/auth'
+ import { InferredCurrentUser } from './all-currentUser'
declare global {
- const mockCurrentUser: (currentUser: CurrentUser) => void
+ const mockCurrentUser: (currentUser: InferredCurrentUser) => void
}
You can try this out in your project as well, hopefully should solve your queries here. |
@dac09 I'm still seeing this issue BTW with latest (3.2.0) |
It's not clear to me whether Redwood aims to support TypeScript's
strict
option (orstrictNullChecks
in particular), but I encountered this issue while experimenting with converting an existing app to Redwood.(Repro of the below if helpful: branch / relevant commit)
Given
strict: true
inweb/tsconfig.json
, and the following code inapi/src/lib/auth.ts
:The following test code (in
web
, e.g. in a component test file) demonstrates some typing issues withmockCurrentUser
:The text was updated successfully, but these errors were encountered: