Skip to content

TypeScript 5.4: NonNullable does not always exclude undefinedย #56644

@eps1lon

Description

@eps1lon

๐Ÿ”Ž Search Terms

NonNullable undefined

๐Ÿ•— Version & Regression Information

  • This changed between versions 5.3 and 5.4.0-dev.20231202

โฏ Playground Link

https://www.typescriptlang.org/play?strict=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitReturns=false&ts=5.4.0-dev.20231202#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgYQgFsiIkB1KTMMNAHgAU4BeOAbwF8A+dgKDkRIAbhADWwOvwHSBAaTihUSACYBnOADkyGgK4AbPZgBGeiVJkW+l69IDa85HHEBPCOjgMAugC4P9zwogSmpwABShAHRRmFAA5r6YSM62ngCULDwiCMrpAD5wOirA6MjAynAA-HDyvkjAQmgA3OY2cBy2Lm4eni0yXAA0LVyhvdLIIuIMUBBgGphEwL6ygzKpvgz+zRy8vMioGDh4APJgMAhkmHoAYoXYZ2RTM1bS6Lf3SI9gFaFrcFnKWx2ymA2EMUDw2DIqngAHcqDQ0L5CCQyJRqLQoHQTu9LjckHdzh9pmAuDs4ei0BFxmJgKEAESvfHvT501K8ckIqBU4Q00KFYElOo5IA

๐Ÿ’ป Code

export interface CommonWrapper<P = {}> {
  invoke<
        K extends NonNullable<
            {
                [K in keyof P]: P[K] extends ((...arg: any[]) => void) | undefined ? K : never;
            }[keyof P]
        >,
    >(
        invokePropName: K,
    ): P[K];
}

interface OptionalFunctionProp {
    functionProp?(): void;
}

declare const wrapper: CommonWrapper<OptionalFunctionProp>

wrapper.invoke("functionProp")
wrapper.invoke(undefined)

๐Ÿ™ Actual behavior

No error

๐Ÿ™‚ Expected behavior

Argument of type 'undefined' is not assignable to parameter of type '"functionProp"'.(2345)

Additional information about the issue

Breaks @types/enzyme tests: https://github.com/DefinitelyTyped/DefinitelyTyped/actions/runs/7057196205/job/19245682552?pr=67570

Metadata

Metadata

Assignees

Labels

Design LimitationConstraints of the existing architecture prevent this from being fixed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions