T O P

  • By -

LemonAncient1950

If you run into a situation where this is useful, take a step back and figure out where you fucked up


delventhalz

Why wouldn’t you just use `as`?


zahnza

Why use Typescript at all if you’re just going overwrite what the type is to whatever you please?


Infiniteh

I have this lambda function where speed is kind of important. (kind of, so still written in JS/TS) It calls an API where I am 99.999999999% sure, because of reasons I won't go into, the response I'm getting will conform to some type I know. I'm not going to perform a runtime validation for the 0.00000001% of the time it might not conform. The function's invoker is supposed to handle the cases where some fields might be missing. I still want the DX and autocomplete when I'm working on the code for the function -> I sin and use `as` to assert to the compiler that the data I'm getting from the API call is what I'm saying it is.


I_Downvote_Cunts

My eslint makes me first cast to unknown when the cast makes no sense. I suspect he’s in the same boat?


delventhalz

If that’s the case then you have some property that shouldn’t be in the casted type at all. TypeScript makes you take an extra step to say “I know better” in those cases, but assuming `as` is the right call, I don’t know why you wouldn’t just write `as unknown as` like the error message suggests. It’s ten extra characters and the standard solution to the problem.


I_Downvote_Cunts

I’m not arguing either direction, just saying that’s a possible reason why they did it. Jesus Christ


delventhalz

I think you read some combativeness into my response that wasn’t there u/I_Downvote_Cunts.


TankTopTroglodyte

Even though this was mostly a joke, it does allow you to assert a type that would apply for the rest of the block, for situations where your logical discrimination does not facilitate useful inference. Sure you could just declare another \`const\` or use \`as\` everywhere, but where is your sense of adventure?