Skip to content

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

@Andarist

Description

@Andarist

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions