Re:TypeScript’s quirks: How inconsistencies make the languag...
by felixfbecker on Friday January 31, 2020 @06:28PM
(#22207058)
Casting can hide errors. It's better to assign it to a variable first that has a type declaration.
An IIFE is safe too:
[ Parent
| on HN ]
Re:TypeScript’s quirks: How inconsistencies make the languag...
by johnghanks on Friday January 31, 2020 @11:12PM
(#22208606)
I would absolutely reject a PR with the IIFE.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by jahewson on Saturday February 01, 2020 @12:09AM
(#22208828)
Actually this cast is safe in TypeScript, additional variables are unnecessary.
The statement ‘const d: Dog’ and the expression ‘d as Dog’ are equivalent w.r.t. type safety. The compiler will error in both cases the if the value is not a Dog.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by agentcooper on Saturday February 01, 2020 @06:56AM
(#22210048)
This is not correct. Following example is valid in strict TypeScript:
interface Dog {
breed: string;
}
const dog = {} as Dog;
console.log(dog.breed);
Using `as` is very unsafe in TypeScript.I've collected more quirks of `as` in this playground: https://www.typescriptlang.org/play/index.html#code/PTAEAMEs...
Re:TypeScript’s quirks: How inconsistencies make the languag...
by BigJono on Friday January 31, 2020 @09:14PM
(#22208101)
Yeah and both of those are less readable, and used everywhere add up to a really crap code base relative to the same code in plain JS.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by horseman on Friday January 31, 2020 @09:29PM
(#22208180)
Yah, pojs is so much nicer because when you mistype breed as bred you get to debug the issue at runtime for a while changing things that are irrelevant when TS could have told you immediately