Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type is not correctly inferred from the function return type when it is a function expression type #52114

Closed
markostanimirovic opened this issue Jan 5, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@markostanimirovic
Copy link

Bug Report

πŸ•— Version & Regression Information

Tested with different TS v4 versions. It does not seem like a regression.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// === Reproduction ===

export type ActionReducer<State> = (state: State | undefined) => State;

export function createReducer<State>(initialState: State): ActionReducer<State> {
    return {} as any;
}

export function createFeature<State>(config: {
    reducer: ActionReducer<State>,
    selectors: (state: State) => unknown,
}) {}

createFeature({
    reducer: createReducer(''),
    // the `state` type is `unknown`
    // if we remove `state` argument the error will disappear
    selectors: (state) => ({}),
    // selectors: () => ({})
});

// === Workarounds ===

// === Workaround 1:
// Define return type as another generic

export function createReducer2<
  State,
  Reducer extends ActionReducer<State> = ActionReducer<State>
>(initialState: State): Reducer {
    return {} as any;
}

createFeature({
    reducer: createReducer2(123),
    // the `state` type is correctly inferred (`number` in this case)
    selectors: (state) => ({})
});

More workarounds are available at the playground link.

πŸ™ Actual behavior

A generic type is not correctly inferred from the function return type when it is a function expression type.

By the way, I noticed that the State type will be correctly inferred when the ActionReducer from the example above is not defined as a function expression type:

// `ActionReducer` is not a function type anymore
export type ActionReducer<T> = T | undefined;

export function createReducer<T>(initialState: T): T {
    return {} as any;
}

export function createFeature<State>(config: {
    reducer: ActionReducer<State>,
    selectors: (state: State) => unknown,
}) {}

createFeature({
    reducer: createReducer(''),
    // the `state` type is correctly inferred
    selectors: (state) => ({}),
});

πŸ™‚ Expected behavior

I'd expect the same behavior when ActionReducer is typed as a function expression.

@Nut41tank
Copy link

+1

@RyanCavanaugh
Copy link
Member

See #47599

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 5, 2023
@markostanimirovic
Copy link
Author

closing as duplicate

@markostanimirovic markostanimirovic closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants