Skip to content

NonNullable not properly casting generic type when excluding nullish valuesΒ #48048

Closed
@flavianh

Description

@flavianh

Bug Report

πŸ”Ž Search Terms

Generic types nonnullable

πŸ•— Version & Regression Information

This bug appears to exist ever since NonNullable was introduced (tested from 3.3.3 to 4.5.4)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function throwIfNullable<T>(
  value: T,
): NonNullable<T> {
  if (value !== undefinedΒ && value !== null) {
    return value;
  }

  throw Error('Nullable')
}

πŸ™ Actual behavior

The line return value raises a compilation error Type 'T' is not assignable to type 'NonNullable<T>'.

πŸ™‚ Expected behavior

No compilation error as the if clause properly guards against nullish values

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions