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)

    Results of a survey. No idea who those guys are or how reliable their data, but I've been wondering for a while now what people mean when they say…

  • (no subject)

    I'm listening to The Ickabog, by J.K. Rowling and read, with much gusto, by Stephen Fry. What started like a nice children's tale not dissimilar from…

  • (no subject)

    From today's questions to the Prime Minister: Mr Speaker, today, millions of Uyghur people in China live in fear under a cruel regime. The BBC,…

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