Skip to content

NonNullable should consider the void type #36703

Closed
@amitport

Description

@amitport

TypeScript Version: 3.7.5

Search Terms: NonNullable void

Code

    function isNonNull<T>(value: T): value is NonNullable<T> {
      return value != null;
    }
    function test(): void | true {
      if (Math.random() < 5) {return true;}
    }
    const t1 = test();
    let t2: true;
    if (isNonNull(t1)) {
      t2 = t1;
    }

Expected behavior:
should work fine, because != null is also not void

Actual behavior:
getting Type 'void' is not assignable to type 'true'.

workaround
I'm using

type NonNullable2<T> = T extends null | undefined | void ? never : T;

which works fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions