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)

    This is a section from Uncle Bob's book that I find particularly bonkers. It is about the daily standup ritual: Standup Meetings Over the years,…

  • (no subject)

    I am sure he meant endothelial. Only not in the sense that Sars-Cov2 is an endothelial virus; but that full-blown covid is an endothelial disease, as…

  • (no subject)

    On the dreadful state of the style of modern Russian communication. "There is no alternative", writes an author, and then immediately outlines an…

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