eelkevdbos / elysia-i18next

MIT License
12 stars 3 forks source link

[Typescript] Type error with elysia v0.8.8 #6

Open hminhman opened 9 months ago

hminhman commented 9 months ago

Related versions:

"elysia": "latest" // 0.8.8
"elysia-i18next": "^3.0.1" // 3.0.1
"i18next": "^23.7.16" // 23.7.16
"typescript": "^5.3.3" // 5.3.3

After using Elysia 0.8.8, the type of t in request param always error. I tried to upgrade all (ts, i18next,...) but can't fix it. And finally I found that the new elysia has defined this

Elysia<BasePath extends string = '', Decorators extends DecoratorBase = {
    request: {};
    store: {};
    derive: {};
    resolve: {};
}, Definitions extends DefinitionBase = {
    type: {};
    error: {};
}, ParentSchema extends RouteSchema = {}, Macro extends Record<string, unknown> = {}, Routes extends RouteBase = {}, Scoped extends boolean = false>

I try to edit directly dist/index.d.ts by adding enough Decorators and RouteBase then issue has gone.

// Current d.ts
export declare const i18next: (userOptions: Partial<I18NextPluginOptions>) => Elysia<"", {
    request: {
        i18n: i18n;
        t: import("i18next").TFunction<[
            "translation",
            ...string[]
        ], undefined>;
    };
    store: {};
}, {
    type: {};
    error: {};
}, {}, {}, false>;

// My fix (has a comment about "translation" too)
export declare const i18next: (userOptions: Partial<I18NextPluginOptions>) => Elysia<"", {
    request: {
        i18n: i18n;
        t: import("i18next").TFunction<[
            "translation", // I think this should be: DefaultNamespace from i18next, because in some case, devs want to custom their namespace
            ...string[]
        ], undefined>;
    };
    store: {};
    derive: {};
    resolve: {};
}, {
    type: {};
    error: {};
}, {}, {}, {}, false>;

p/s: This is a good and helpful lib. Thanks for your support.

eelkevdbos commented 8 months ago

@hminhman, thank you for the report. Can you post the actual typescript error thrown? I've noticed a few typing errors appearing and disappearing the past versions of Elysia, so I'm hoping to pinpoint the exact cause.

hminhman commented 8 months ago

Hi @eelkevdbos , When I use: (it's ok with old elysia or add my above comment) .use(i18next({initOptions: {lng: LANGUAGE.en, ...language}})) then access t: .post('/login', async function login({body, jwt, t}) { } I face this error: image

The reason in my opinion is: new elysia (>0.8.8) update their generic type. They added "derive, resolve" and more field in their "Decorators" so elysia-i18next out of date here:

{
    request: {
        i18n: i18n;
        t: import("i18next").TFunction<[
            "translation",
            ...string[]
        ], undefined>;
    };
    store: {};
         // MISSING derive and resolve
}

My solution:

// My fix (has a comment about "translation" too)
export declare const i18next: (userOptions: Partial<I18NextPluginOptions>) => Elysia<"", {
    request: {
        i18n: i18n;
        t: import("i18next").TFunction<[
            "translation", // SHOULD CHANGE: I think this should be DefaultNamespace from i18next, because in some case, devs want to custom their namespace
            ...string[]
        ], undefined>;
    };
    store: {};
    derive: {}; // ADD THIS
    resolve: {}; // AND THIS
}, {
    type: {};
    error: {};
}, {}, {}, {}, false>; // ADD a missing empty object here too

Thank you.