Skip to content

add IsNothing() type guard #988

@unional

Description

@unional

🚀 Feature Proposal

Add IsNothing() type guard that can exclude Nothing from a union type.

Motivation

The following does not work:

let x: string | Nothing | undefined

if (x === nothing) {
  // x type should be `Nothing`
}
else {
  // x type should be string
}

Since the Nothing type is implemented as a class, the x === nothing will not remove the Nothing type from the control flow analysis.

Adding a IsNothing() type guard will solve this problem.

Can this be solved in user-land code?

I can implement that easily in user-land, but this is a core concept of immer and IMO should live inside immer.

I can contribute to the project. Just want to know which file should I add the tests to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions