3 ms·
Wouldn't it be string|number unless using the `as const` modifier?
by IggleSniggle 6y ago
Wouldn't it be string|number unless using the `as const` modifier?
- drew-y 6y agoIt 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() } ```