Andrey (azangru) wrote,

I love typescript. But right now, I am swearing at the screen, because I can't understand the logic of it.

Typescript has a keyword extends. Which can be used in this traditional context, where a class / object / interface extends an interface:

In this case, we are saying that whatever we are declaring (Square, in this example) will have all the properties declared on the parent interface, and may add some more. Surely, it produces a superset of types conforming to the ancestral interface and adding some extra properties. This is what the word to extend means.

But at the same time, typescript can do this:

What the hell? The word extends is used here to define a subset of a type?! I.e. in a completely opposite sense than in the previous example? How can you even use the word "to extend" to mean "one of"?

Then, of course, we have this:

A nice generic function, whose type could be written like this:

or should it be like this:

No, really, both notations are valid; they must mean different things; in this particular case I want type Bar; but when do I want type Foo? The first type definition (for Foo) makes the function a generic type; so when a variable of this type is declared, another type has to be passed to the generic. The second notation (for Bar) is not describing a generic type. Is it "generic constraints" as mentioned in Typescript Handbook? I don't know; the handbook is not using arrow functions, and I cannot see how the types defined in the section on generic constraints are different than the types defined in the section on generic functions.

Despite its huge popularity, documentation for typescript leaves a lot to be desired.

  • (no subject)

    A good episode of the Rebel Wisdom podcast where they invited Yuri Deigin to present an argument against Bret's ideas on vaccines and ivermectin.…

  • (no subject)

    Another interesting example of a gap in sense-making. "How do you test that this is the delta variant," asks a journalist the deputy White House…

  • (no subject)

    Two clips left a particularly deep impression on me over the last couple of days. One is Pelosi's reply to a journalist: Not the first of her…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.