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 not inferred inside mapped type parameter #29479

Open
OliverJAsh opened this issue Jan 18, 2019 · 6 comments · May be fixed by #52737
Open

Generic not inferred inside mapped type parameter #29479

OliverJAsh opened this issue Jan 18, 2019 · 6 comments · May be fixed by #52737
Labels
Bug A bug in TypeScript Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution
Milestone

Comments

@OliverJAsh
Copy link
Contributor

TypeScript Version: 3.2.2

Search Terms: generic inference {} createStructuredSelector mapped types

Code

type Selector<S, R> = (state: S) => R;

declare function createStructuredSelector<S, T>(
    selectors: { [K in keyof T]: Selector<S, T[K]> },
): Selector<S, T>;

type State = { foo: number };
declare const mySelector: Selector<State, boolean>;

// Workaround: use explicit generics
// const result = createStructuredSelector<State, { mySelector: boolean }>({

// `State` generic incorrectly inferred as `{}`.
const result = createStructuredSelector({
    // Unexpected type error under `strictFunctionTypes` due to bad inference (above)
    mySelector,
});

result;
@weswigham weswigham added Bug A bug in TypeScript Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution labels Jan 18, 2019
@Kingwl
Copy link
Contributor

Kingwl commented Jan 21, 2019

An interesting thing:

type Selector<S, R> = (state: S) => R;

declare function createStructuredSelector<S, T, L extends keyof T = keyof T>(
    selectors: {
        [K in L]: Selector<S, T[K]>
    }
): Selector<S, T>;

type State = { foo: number };
declare const mySelector: Selector<State, boolean>;

// result => Selector<State, {}>
const result = createStructuredSelector({
    mySelector
});

@Kingwl
Copy link
Contributor

Kingwl commented Jan 21, 2019

i have already fixed the case whitch @OliverJAsh has provided, but i have no idea about mine case

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@ilyaulyanov
Copy link

Any updates on this? I'd rather avoid duplication in generics

@RyGuy101
Copy link

RyGuy101 commented Sep 1, 2019

One workaround is to use function currying so you only have to specify the first parameter:

// I put this in a file called 'reduxUtils.ts'
import * as Reselect from "reselect";

export function createStructuredSelector<S>() {
  return function<T>(
    selectors: { [K in keyof T]: Reselect.Selector<S, T[K]> }
  ) {
    return Reselect.createStructuredSelector<S, T>(selectors);
  };
}

Usage:

const result = createStructuredSelector<State>()({ mySelector });

@borispinus
Copy link

borispinus commented Feb 8, 2020

now it's unknown, not {}. Is it typescript bug that in this case unknown and State are not compatible? Or expected behavior?

@Andarist
Copy link
Contributor

The interesting thing is that if we add more parameters to Selector (even non-generic ones) then an inline version that doesn't use those parameters can be inferred OK:

type Selector<S, R> = (state: S, meta: number) => R;

declare function createStructuredSelector<S, T>(selectors: {
  [K in keyof T]: Selector<S, T[K]>;
}): Selector<S, T>;

type State = { foo: number };

declare const mySelector: Selector<State, boolean>

const result1 = createStructuredSelector({
  // Type 'Selector<State, boolean>' is not assignable to type 'Selector<unknown, boolean>'.
  //   Type 'unknown' is not assignable to type 'State'.(2322)
  mySelector: mySelector,
});

const result2 = createStructuredSelector({
  // ok
  mySelector: (state: State) => true,
});

result2;
// ^? const result: Selector<State, { mySelector: boolean; }>

export {};

If we just remove meta from Selector (that stays unused throughout this whole example) then both fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants