Re:Bad TypeScript Habits
by eyelidlessness on Tuesday February 02, 2021 @11:25PM
(#26010602)
> “as unknown as MyType” usually works almost as well and avoids throwing you out into the cold untyped darkness.
No it doesn’t. You’re casting to `any` and hiding that fact. Both top types can be anything at all, `unknown` is only safer if you narrow it to something else by testing it. If you cast it to something else you’re just treating `unknown` as `any`.
No it doesn’t. You’re casting to `any` and hiding that fact. Both top types can be anything at all, `unknown` is only safer if you narrow it to something else by testing it. If you cast it to something else you’re just treating `unknown` as `any`.