註冊並分享邀請連結,可獲得影片播放與邀請獎勵。

Vanilagy
@vanilagy
It's pronounced vanilla-gee | Author of Mediabunny and Marble Blast Web. Love working on technically challenging projects that bring the best out of the web.
加入 July 2014
119 正在關注    3.9K 粉絲
PLEASE stop modeling "errors as values" in TypeScript after the "Result" enum from Rust. It is unnecessarily verbose and you're paying for an object instantiation for every value you return, everywhere, even if the error path never happens in the entire lifetime of your application. Instead, use unions: Simply return T | Error, and TypeScript automatically narrows the type for you once you check (x instanceof Error). instanceof checks are trivial, and if you never return errors at runtime, V8's JIT will optimize out the error case entirely and performance will be identical to if you didn't handle errors at all. You'll only pay for the deopt when you do hit the error case, but that's fine.
顯示更多
0
42
369
13
轉發到社區