Skip to content

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

Open

Description

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 Anders Hejlsberg (@ahejlsberg)

Activity

  1. typescript-bot commented on Oct 27, 2022

    @typescript-bot
    Contributor

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

  2. ahejlsberg commented on Dec 2, 2023

    @ahejlsberg
    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.

  3. added
    BugA bug in TypeScript
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Dec 2, 2023
  4. pkudryashov commented on Jul 3, 2024

    @pkudryashov

    I have similar issue in 5.4.x and 5.5.x

    type TypePropertyNames<T, U> = NonNullable<{[K in keyof T]: NonNullable<T[K]> extends U ? K : never}[keyof T]>;
    
    interface Test {
        field1?: boolean;
        field2: boolean;
        field3: string;
    }
    type TestFields = TypePropertyNames<Test, boolean>;
    // should be 'field1' | 'field2'
    // but now 'field1' | 'field2' | undefined
    
    type TestFields2 = NonNullable<TypePropertyNames<Test, boolean>>; // this works correct
    Output
    "use strict";
    Compiler Options
    {
      "compilerOptions": {
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "strictPropertyInitialization": true,
        "strictBindCallApply": true,
        "noImplicitThis": true,
        "noImplicitReturns": true,
        "alwaysStrict": true,
        "esModuleInterop": true,
        "declaration": true,
        "target": "ES2017",
        "jsx": "react",
        "module": "ESNext",
        "moduleResolution": "node"
      }
    }

    Playground Link: Provided

    Is this the same bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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