3 ms·
In TypeScript, x would be of type () => "abc" | number.
by drew-y 6y ago
In TypeScript, x would be of type () => "abc" | number.
- IggleSniggle 6y agoWouldn'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() } ```