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)

    Today I learned that the word maidan exists in English, and that it is of Arabic origin. I've never heard it other than in the Ukranian context of…

  • (no subject)

    A short report by Leonid Rogozov about how he operated on himself to remove his own appendix in Antarctica. Published in English in the Soviet…

  • (no subject)

    Great opening paragraphs by Chris. The first one captures what I've been feeling about the subject, but haven't found the words to express it so…

  • 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.