unsplash / intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
MIT License
56 stars 3 forks source link

Prevent tsc from narrowing nested switch statements v2 #134

Closed samhh closed 2 years ago

samhh commented 2 years ago

Fixes #111 by annotating plain switch conditions with as typeof. Supercedes #133 which solved the same problem with IIFEs. A test which is fixed by the final commit is included as suggested.

Includes some refactoring to try and make the JS compiler a bit more extensible. Not entirely sold on this approach but it works without sacrificing the JS compiler's independence.

OliverJAsh commented 2 years ago

In the future we might be able to use satisfies operator instead of a type assertion: https://github.com/microsoft/TypeScript/issues/47920#:~:text=hopefully%20become%20obvious.-,Safe%20Upcast,-Frequently%20in%20places