immer icon indicating copy to clipboard operation
immer copied to clipboard

add `IsNothing()` type guard

Open unional opened this issue 1 year ago • 0 comments

🚀 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.

unional avatar Oct 30 '22 01:10 unional