supertokens / supertokens-node

Node SDK for SuperTokens core
https://supertokens.com
Other
292 stars 79 forks source link

JSONObject type used for metadata doesn't accept typescript interface #857

Open markusenglund opened 3 months ago

markusenglund commented 3 months ago

The following code causes this ts error:

Argument of type 'UserMeta' is not assignable to parameter of type 'JSONObject'. Index signature for type 'string' is missing in type 'UserMeta'

        import UserMetadata from 'supertokens-node/recipe/usermetadata';

        // ...later
        interface UserMeta {
            foo: string;
        }

        const userMetadata: UserMeta = {
            foo: 'bar',
        };

        await UserMetadata.updateUserMetadata(
            response.user.id,
            userMetadata, // Argument of type 'UserMeta' is not assignable to parameter of type 'JSONObject'. Index signature for type 'string' is missing in type 'UserMeta'.
        );

Switching from interface to a type works ok:

        type UserMeta = {
            foo: string;
        };

I haven't made the effort to figure out why, but if seems like incorrect behavior.

rishabhpoddar commented 3 months ago

We generally prefer using type instead of interface as types are more flexible. I don't think this is an issue with our SDK though.

markusenglund commented 3 months ago

Your preferences might not be shared by the consumers of your library. In the project I'm working on, we need to use a class as the type of the metadata due to the framework we're using. Type-casting to appease the ts compiler is totally fine for us, I'm just letting you know that this is an annoyance.

rishabhpoddar commented 3 months ago

Fair enough. We will see what we can do about this if more people face this same issue.