Skip to content

Arrow functions behave inconsistently with classic functionsΒ #56067

@Ryuurock

Description

@Ryuurock

πŸ”Ž Search Terms

Unable to get generic type, arrow function

πŸ•— Version & Regression Information

  • This is a crash
  • This problem exists in Nightly version
  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzDigxAB4AoeK+AZQ2IRAA8TVgBneAJXBxmFLsMMLKgDmAGnhRUATwB8EytQAixKPGasO3Xv0HDRk6XMXKqAQUy5UnLSDacAFEIYAuWvRJTg6j2voASngAXnldMD4BIRFxKRkFJXknWDF2DwBvczEQDDp3eCdgsM8GAG5zKGs8dPgrbBqK6hyMAKgPItDwtoqAX0CPADccLGAK8kIQBics6ngW-JJO2bnqOAxkGHwM6Q8ARnhe80OlOZa2juLwldX4dc3t+AAjDwAmQ+Pe0+oqhtsXLwgHzqYI3VauEjHOYAemh8AAegB+KHUXz0FFUWEI5G3Kj3LbwMG4+AAL32GI+cyO-SAA

πŸ’» Code

declare function create<
    State extends Record<string, any>,
    Data extends Record<string, any>,
    Actions extends (state: State, data: Data) => Record<string, any>,
>(args: {
   getState: () => State;
   actions: Actions;
   getData: () => Data;
}): void;

create({
    getState() {
        return { a: 1 }
    },
    getData: () => {
        return { b: 2 }
    },
    actions(state, data) {
        state
        // ^?
        data
        // ^?
        return {
            z: 1
        }
    }
})

πŸ™ Actual behavior

The getState and getData function types need to be consistent. For example, they are both arrow functions or ordinary functions to correctly infer the type. Otherwise, they cannot

πŸ™‚ Expected behavior

No matter what type of functions getState and getData are, the types can be correctly inferred in the actions function parameters.

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