TypeScript’s quirks: How inconsistencies make the language more complex
1–10 of 216 posts
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#2Java: int x
Cpp: int x
C: int x
Some fucking retards at MS derp corp... TypeScrap:
x: int
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#3The correct use of:
printDog({
breed: "Airedale",
age: 3
})
is to explicitly cast it: printDog({
breed: "Airedale",
age: 3
} as Dog)
[0] https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&p...[1] https://www.typescriptlang.org/docs/handbook/advanced-types....
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#4The first example doesn't bother me and I think it's a weak argument. TypeScript allows literals[1] as types[0] so it is trying to use the object as a type. The correct use of: printDog({ breed: "Airedale", age: 3 }) is to explicitly cast it: printDog({ breed: "Airedale", age: 3 } as Dog) [0] https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&p... [1] https://www.typescriptlang.org/docs/handbook/advanced-type…
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 language more complex
#5Re: TypeScript’s quirks: How inconsistencies make the language more complex
#6The first example doesn't bother me and I think it's a weak argument. TypeScript allows literals[1] as types[0] so it is trying to use the object as a type. The correct use of: printDog({ breed: "Airedale", age: 3 }) is to explicitly cast it: printDog({ breed: "Airedale", age: 3 } as Dog) [0] https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&p... [1] https://www.typescriptlang.org/docs/handbook/advanced-type…
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#7Edit: The article says "magical hidden properties".
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#8How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#9How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".
type CompanyID = string & {readonly brand: unique symbol}
type OrderID = string & {readonly brand: unique symbol}
type UserID = string & {readonly brand: unique symbol}
type ID = CompanyID | OrderID | UserID
function CompanyID(id: string) {
return id as CompanyID
}
...
TypeScript can be very confusing sometimes, and given the learning curve I’d caution someone trying to learn modern JS away from starting out with it. But in even small codebases it’s an amazing improvement in safety, especially for refactoring.[1] https://learning.oreilly.com/library/view/programming-typesc...
Re: TypeScript’s quirks: How inconsistencies make the language more complex
#10How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".