Skip to content

Function generic unmatched: Matching hehavior is difference between arrow functions and normal functionΒ #58630

@HoikanChan

Description

@HoikanChan

πŸ”Ž Search Terms

Function generic unmatched

πŸ•— Version & Regression Information

  • This is marybe bug?
  • The versions is 5.4.5

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/KYDwDg9gTgLgBDAnmYcDKMCGNgDECuAdgMYwCWEhAPBtsAHxwC8cAFAFBxdwDOAXOiw4ANJ24A6SZigBzfnEyFEAbQC67AJTNGixAG527UJFgJkqAGr5QGaMADyYcpR5UxXWiPdwAshAAm+AA2wDyijCwA3t48QsAA-AKeqAA+bKxaTIzJWmmRbBpJcXAAvgbccAC2+EIUhDyJcABKwMTQ-lSxUGSEMsKCdAQkztTJ9PTl3JUBwaGN0RUVygDWcD1wy8CIEABmvjMhPKoCVjYwdo4jrn6Bhyuq-YTAAG7AUPTeZewlhsbQ8DsiKQ6nBiFBgHRbOC3BVknBQDhCP4eM1Wu1OjBur1+kRloQIAB3Qj0UQVG6zFEI4BIlEtNpQDpdHp9VH0xmY5k4wh4wnEknseisCBOOryU4gKEOEUuGhxfrkw70LTRH7sNr1eCxOzMUHgyHncGsBbcWJ0DJwfLgmD4KCEC1wABG0gATAIAIylUqkqY1bCigT5HrEASsU04N3OgDMmUYYeAEcj4idUGdAGpU17vNNbqEA94KjsIBA84tS3AAPTl3hxEMxtj5ZOuuAekoafqVuAAFQAFmQUVVsMRuwhu6g45My9wO3GBIDhiDzcbJ1wOxUrTa7Q2Xe7ShPlx2Su2qwAhGojvugxQAchg+bLccXd+XXHXtvtjZ3X2fXEPFarin8LtexRYhr1vb8-2rOgA0dbdmy9OAn0WapahcGCgxDOM6zjJMXXTTMkMPT5RFbAwgA

πŸ’» Code

export type StateFunction<State> = (
    s: State,
    ...args: any[]
) => any;

export type VuexStoreOptions<
    State,
    Modules,
> = {
    state?: State | (() => State) | { (): State };
    mutations?: Record<string, StateFunction<State>>;
    modules?: {
        [k in keyof Modules]: VuexStoreOptions<Modules[k], never>
    };
}

export function createStore<
    State extends Record<string, unknown>,
    Modules extends Record<string, Record<string, unknown>>,
>(options: VuexStoreOptions<State, Modules>) {
}

const store = createStore({
    state() { return { bar2: 1 } },
    mutations: { inc: (state123) => state123.bar2++ },
    modules: {
        foo: {
            // state: () => ({ bar2: 1 }), // This  match the state;
            // state: function () {
            //     return { bar2: 1 };
            // }, // But this can't
            state() {
                 return { bar2: 1 };
            }, // and This can't
            // state: { bar2: 1 }, 
            mutations: { inc: (state) => state.bar2++ },

        },
    },
});

πŸ™ Actual behavior

If use arrow function or obj method function to declare the state in modules.foo

Got error:
'state.bar2' is of type 'unknown'.

But arrow function won't.

πŸ™‚ Expected behavior

No matter which one i use to declare the fucntion ( arrow function, method function or just function),
the state can be known in mutations param

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions