supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.86k stars 220 forks source link

TypeScript: Property 'email' does not exist on type 'SignInWithPasswordCredentials' #958

Closed JorensM closed 3 months ago

JorensM commented 3 months ago

Bug report

Describe the bug

I'm trying to create a mock implementation of supabase.auth.signInWithPassword for my tests, but for some strange reason I'm getting a type error. I'm not sure if this is a bug with TypeScript or Supabase, but from looking at the type definition of SignInWithPasswordCredentials, I'm not sure why the error would be shown.

To Reproduce

async (credentials: SignInWithPasswordCredentials) => {
        if(credentials.email == TEST_EMAIL && credentials.password == TEST_PASSWORD) { // Type error here that property 'email' does not exist on type
            _setUser(createSupabaseUser(credentials.email))
            return true;
        } else {
            throw new AuthError('Incorrect email/password', 400);
        }
}

Autocomplete also doesn't suggest me the email property

Expected behavior

Shouldn't show type error.

Screenshots

error message

System information

andriishupta commented 3 months ago

@JorensM, this is not a bug, this is how Typescript's UNION types work:

image

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing

if it could be either "email" or "password" - type narrowing is required Check SignInWithPasswordCredentials type to "see" how it is { email, ... } | { phone, ... }

JorensM commented 3 months ago

@andriishupta I am aware how union types work, but I'm getting a type error even when adding a type guard. Additionally I'm not getting any autocomplete for the email and phone properties, which should be there.

andriishupta commented 3 months ago

@JorensM it knows that you definitely have password:

image

and after narrowing it knows that you have an email:

image

in your specific example, it is not counted as "type narrowing": credentials.email == TEST_EMAIL && credentials.password == TEST_PASSWORD

if you face something else with such an example as I have provided - the issue is still open, but still unique to your case / IDE / setup

just wanted to help :) GL!

JorensM commented 3 months ago

@andriishupta Thanks, I appreciate your help, I was just saying how it is for me, didn't want to sound rude or anything. I did exclude the type guard from my sample code, but in my IDE the email property wasn't showing up even after type narrowing, but it seems to be working correctly for you, so it must just be an issue on my end.

kangmingtay commented 3 months ago

hey @JorensM, did you manage to fix your issue? it does look like an IDE specific problem here, maybe a VSCode update or restart is due 🙃

we do have the field export in the type here for a peace of mind: https://github.com/supabase/gotrue-js/blob/15c7c8258b2d42d3378be4f7738c728a07523579/src/lib/types.ts#L492

will be closing this issue but do let us know here if you've found a workaround!

JorensM commented 3 months ago

@kangmingtay Hey, sorry I forgot to close this myself, yes the issue went away after updating VSCode.