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.
const dog: Dog = {
breed: "Airedale",
age: 3
}
printDog(dog)
An IIFE is safe too: printDog(((): Dog => ({
breed: "Airedale",
age: 3
})())
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
Re:TypeScript’s quirks: How inconsistencies make the languag...
by lucasmullens on Friday January 31, 2020 @06:29PM
(#22207068)
The article is just describing quirks, not arguing against them. Specifically for the first example they said "I think that the TypeScript stance here isn’t wrong".
Re:TypeScript’s quirks: How inconsistencies make the languag...
by mekster on Friday January 31, 2020 @08:22PM
(#22207824)
Casting is as bad as using "any". You can make the compiler think anything you throw at it as that type and you can get bitten at runtime.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by alexcole on Friday January 31, 2020 @08:49PM
(#22207968)
Thats not 100% true in TypeScript. TypeScript actually only allows "up-casts" and "down-cast" (but if the type is neither a supertype nor a subtype the compiler will throw an error).
Re:TypeScript’s quirks: How inconsistencies make the languag...
by seanmcdirmid on Friday January 31, 2020 @10:16PM
(#22208400)
Ya, I often have to write “this as any as T” because I’m using T as a self type.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by nojvek on Friday January 31, 2020 @09:52PM
(#22208292)
That’s my understanding too. You just can’t cast types wily Nily.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by jahewson on Saturday February 01, 2020 @12:08AM
(#22208820)
Nope. This is not how casting works in TypeScript. To get an unsafe cast you have to go via the ‘any’ or ‘unknown’ types.
e.g.
‘foo as unknown as Dog’
Re:TypeScript’s quirks: How inconsistencies make the languag...
by okbake on Saturday February 01, 2020 @03:15PM
(#22212943)
There are a few casts that are allowed that I would consider unsafe, particularly around unions of literals or using empty object literals:
// No error here
type Animal = 'dog' | 'cat';
const notAnimal = 'couch' as Animal;
// No error here either
type Food = { isSpicy: boolean };
const empty = {} as Food;
https://www.typescriptlang.org/play/index.html#code/C4TwDgpg...
Re:TypeScript’s quirks: How inconsistencies make the languag...
by Kinrany on Friday January 31, 2020 @09:19PM
(#22208125)
Casting may hide errors and is unnecessary in this case: `as const` will work just as well.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by potatoz2 on Friday January 31, 2020 @09:53PM
(#22208295)
The correct use is to remove the `age` property in this case: if you're not keeping a reference to the object or that property and the called function cannot use it, why have it in the first place?
Re:TypeScript’s quirks: How inconsistencies make the languag...
by alexcole on Friday January 31, 2020 @11:33PM
(#22208687)
That’s not always true. If you are going to print the object to the console or send it in a network request it’s possible that the additional property is important.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by wvenable on Friday January 31, 2020 @11:56PM
(#22208782)
But then it should be in the type signature of the object. TypeScript is signalling this as an error because it's a literal passed directly to the function and that property (theoretically) can never be used because it wasn't declared.
Re:TypeScript’s quirks: How inconsistencies make the languag...
by nojvek on Friday January 31, 2020 @09:50PM
(#22208283)
Why would casting hide errors?
Re:TypeScript’s quirks: How inconsistencies make the languag...
by IggleSniggle on Saturday February 01, 2020 @03:04AM
(#22209357)
In the simple case, if it could be Dog|Cat and you cast as Dog but you get a Cat, and TS could have told you about the error if it hadn't been forced to treat it as Dog. This can be fixed by having your types be derived by runtime validation.
The correct use of:
is to explicitly cast it: [0] https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&p...[1] https://www.typescriptlang.org/docs/handbook/advanced-types....