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

Broken keyof inference #46870

Closed
DScheglov opened this issue Nov 19, 2021 · 3 comments
Closed

Broken keyof inference #46870

DScheglov opened this issue Nov 19, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@DScheglov
Copy link

Bug Report

πŸ”Ž Search Terms

keyof

πŸ•— Version & Regression Information

  • This changed between versions 4.4.4 and 4.5.2

Also the bug was reproduced in the Playground with version 4.5.0-beta

⏯ Playground Link

Playground (v4.5.0-beta)

πŸ’» Code

import React from "react";

type SetState<S> = {
    (value: S): void;
    (reducer: (currentState: S) => S): void;
};

type StateManager<S> = {
    getState(): S;
    setState: SetState<S>;
    subscribe(listener: (value: S) => void): () => void;
};

type StateType<SM> = SM extends StateManager<infer S> ? S : never;

type KeyOfType<S extends {}, T> = {
    [P in keyof S]: S[P] extends T ? P : never;
}[keyof S];

export function useStateManager<S>({ getState, setState, subscribe }: StateManager<S>): [S, SetState<S>] {
    const [, next] = React.useReducer((v: number) => v + 1, 0);
    React.useEffect(() => subscribe(() => next()), []);

    return [getState(), setState];
}

export function useStateManagerFromContext<C extends {}, N extends KeyOfType<C, StateManager<any>>>(
    context: React.Context<C>,
    stateManagerName: N
) {
    const ctx: Pick<C, KeyOfType<C, StateManager<any>>> = React.useContext(context);
    return useStateManager<StateType<C[N]>>(ctx[stateManagerName]);
}

πŸ™ Actual behavior

The Error reported and compilation failed:

Errors in code
Argument of type 'C[N]' is not assignable to parameter of type 'StateManager<StateType<C[N]>>'.
  Type 'C[KeyOfType<C, StateManager<any>>]' is not assignable to type 'StateManager<StateType<C[N]>>'.
    Type 'C[C[keyof C] extends StateManager<any> ? keyof C : never]' is not assignable to type 'StateManager<StateType<C[N]>>'.
      Type 'C[keyof C]' is not assignable to type 'StateManager<StateType<C[N]>>'.
        Type 'C[string] | C[number] | C[symbol]' is not assignable to type 'StateManager<StateType<C[N]>>'.
          Type 'C[string]' is not assignable to type 'StateManager<StateType<C[N]>>'.

πŸ™‚ Expected behavior

No error reported as it works before, as instance in v4.4.4:

Playground (v4.4.4)

@yseymour
Copy link

Minimal playground repro

@yseymour
Copy link

I think this is the same as #46855.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 19, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants