3 ms·
Casting and `as unknown as <type>` are just as useful, probably more so because they specify what the type _should_ be.
by caesil 3y ago
Casting and `as unknown as <type>` are just as useful, probably more so because they specify what the type _should_ be.
- ravenstine 3y agoIMO, it's a million times better than accepting something as any, if the latter is possible to avoid. Sadly, every Typescript codebase I've worked with is riddled with `any`, and developers seem skeptical of `as`. That doesn't make sense to me. Throwing your hands up and saying "whatever" is better than declaring what the type should be when Typescript can't infer it otherwise?
- OJFord 3y agoI don't really use JS or TS, so sorry if it should be obvious, but why is that so much better? You just move the (incorrect) assumption from the logic to the type annotation? And you're still not going to get a compile-time error for it, because you're explicitly telling the compiler to make that assumption?
- orwin 3y agoI didn't know about 'as' until right now, because our codebase doesn't use it (we create a lot of weird unions instead),and TS is like 5% of what we do (and we are very small). Thank you, i'll try to see if i can use it when needed, it's a detail but can save me hours imho.
- deleted 3y ago[deleted]