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

NonNullable is not resolved for a mapped type in a contextual parameter type from instantiated type alias #51331

Open
Andarist opened this issue Oct 27, 2022 · 2 comments Β· May be fixed by #56652
Assignees
Labels
Bug A bug in TypeScript

Comments

@Andarist
Copy link
Contributor

Andarist commented Oct 27, 2022

Bug Report

πŸ”Ž Search Terms

NonNullable, Omit, mapped type, contextual type

πŸ•— Version & Regression Information

  • This changed between versions 4.7 and 4.8

⏯ Playground Link

Playground link with relevant code
Original repro case

πŸ’» Code

type GestureKey = "drag";
type DragState = { movement: [number, number]; };

interface State {
  drag?: DragState;
}

type SharedGestureState = {
  dragging?: boolean;
};

type FullGestureState<Key extends GestureKey> = SharedGestureState &
  NonNullable<State[Key]>;

type Handler<Key extends GestureKey> = (
  state: Omit<FullGestureState<Key>, "event">
) => void;

const works = (state: Omit<FullGestureState<"drag">, "event">) => {
  state;
  // ^? (parameter) state: Omit<SharedGestureState & DragState, "event">
  
  type StateKeys = keyof typeof state;
  //   ^? type StateKeys = "dragging" | "movement"
  console.log(state.movement);
};

const doesntWork: Handler<"drag"> = (state) => {
  state; (parameter) state: Omit<SharedGestureState & DragState, "event">
  // ^? (parameter) state: Omit<SharedGestureState & DragState, "event">

  type StateKeys = keyof typeof state;
  //   ^? type StateKeys = "dragging"
  console.log(state.movement); // Property 'movement' does not exist on type 'Omit<SharedGestureState & DragState, "event">'.(2339)
};

πŸ™ Actual behavior

We can observe a type error in the doesntWork function and the keyof reporting incorrect result there for the given parameter type.

πŸ™‚ Expected behavior

There should be no error as the type for the state parameter in both functions is the same.

cc @ahejlsberg

@typescript-bot
Copy link
Collaborator

The change between release-4.6 and main occurred at 51b346d.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Oct 27, 2022
@ahejlsberg
Copy link
Member

The core issue here is the normalization we perform on index types. Specifically, we generally normalize keyof (A & B) into keyof A | keyof B. Turns out that doesn't always hold when A and B are generic types:

type Foo = { x: string } | undefined;
type Bar = { y: string };

type FooAndBar = Foo & Bar;

type Keys<T, U> = keyof (T & U);  // Normalized into keyof T | keyof U

type K1 = keyof Foo;  // never
type K2 = keyof Bar;  // "y"
type K3 = Keys<Foo, Bar>;  // "y"
type K4 = keyof (Foo & Bar);  // "x" | "y"

Above, keyof Foo | keyof Bar is just "y", whereas keyof (Foo & Bar) is "x" | "y" because Foo & Bar is reduced to just { x: string} & { y: string }, thus eliminating the undefined type.

We already have logic in place to defer types like keyof (T & {}), but it looks like we need to defer all keyof (T & U) where one or both of the types are generic. We can then simplify the type to keyof T | keyof U where appropriate. I'm going to put together a PR and we'll see what the effects are.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Dec 2, 2023
@ahejlsberg ahejlsberg linked a pull request Dec 3, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants