Re:Hindley-Milner Type Inference (2012)
by IggleSniggle on Friday July 10, 2020 @04:57PM
(#23796366)
Wouldn't it be string|number unless using the `as const` modifier?
[ Parent
| on HN ]
It used to be. But more recent versions of TypeScript (I believe since 3.5) are much stricter with strings in implicit unions. It's really quite helpful.
Here's an example where the feature can save you:
```
let x = () => {
if (Math.random() > 0.5) {
return "abc"
} else {
return 4
}
}// ERROR: This condition will always return 'false' since the types '"abc" | 4' and '"sdf"' have no overlap.
if (x() === "sdf") {
do_work()
}```