Live data from Hacker News

TypeScript’s quirks: How inconsistencies make the language more complex

blog.asana.com

1–10 of 216 posts

Re: TypeScript’s quirks: How inconsistencies make the language more complex

#3
The 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-types....

Re: TypeScript’s quirks: How inconsistencies make the language more complex

#4
post #3

The 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…

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 language more complex

#5
I don’t agree with the article’s claim that discriminated unions are just a JS compat feature. Many APIs type JSON serialised entities using a string property and this feature makes it very easy to write interfaces and code to work with them.

Re: TypeScript’s quirks: How inconsistencies make the language more complex

#6
post #3

The 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…

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 language more complex

#8

How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".

You can hide some of your structure using "private". There are also branded types: https://github.com/spion/branded-types

Re: TypeScript’s quirks: How inconsistencies make the language more complex

#9

How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".

Programming TypeScript has a section on “Simulating Nominal Types” [1]. You define a type that’s impossible to create naturally, and provide a function that asserts something is of that type.

    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

#10

How does TypeScript ensure encapsulation if it doesn't have nominal types? Edit: The article says "magical hidden properties".

You can use private to do it OO/Java style, but you don't need typescript for that: ES6 modules let you encapsulate stuff just fine without any type features.
Post reply on HN