arthurfiorette / proposal-safe-assignment-operator

Draft for ECMAScript Error Safe Assignment Operator
https://arthur.run/proposal-safe-assignment-operator/
MIT License
1.13k stars 12 forks source link

As-is, it is impossible to accurately refine types in a generic context #17

Closed rbalicki2 closed 3 weeks ago

rbalicki2 commented 1 month ago

This makes it impossible to refine the type in a generic function

One response: you should never throw null.

But a discriminated union also allocates an object

Conclusion

gabrielricardourbina commented 3 weeks ago

This is already solved in Promise.allSettled

arthurfiorette commented 3 weeks ago

Is throw null a common pattern? Not directly. 6.1k results on GitHub.

That's less than 0.1% and almost all 6.1k results comes from big libraries like nextjs, which uses throw null for different reasons.

arthurfiorette commented 3 weeks ago

[T, null] | [null, E] is the wrong type. As you can see with tuple-it. The correct type should be [E] | [null, T]

arthurfiorette commented 3 weeks ago

Related to https://github.com/arthurfiorette/proposal-safe-assignment-operator/issues/30

DScheglov commented 3 weeks ago

@arthurfiorette

The type [E] | [null, T] is also incorrect: TS Playground, the type parameter E makes the discriminating this type impossible.

The correct type is [{}] | [null, T]: TS Playground

rbalicki2 commented 3 weeks ago

Anyway, @DScheglov is right about the inability to discriminate in a generic context with that type.