Live data from Hacker News

Functional Programming with TypeScript's Type System

desislav.dev

21–30 of 46 posts

Re: Functional Programming with TypeScript's Type System

#21
post #15

Earlier quoted context omitted.

Sorry, I don't get it. What do you expect to happen here?

Presumably the issue is that a Readonly shouldn't be a subtype of Foo I should note that I haven't yet had the pleasure of using a language that handles const-ness properly, as Readonly should be neither a subtype nor a supertype of T

As an aside, I'm on mobile and tried to visit the TypeScript playground to play around with this, but weirdly, the default code is an implementation of FizzBuzz! There was not an obvious way to clear it to get a blank editor. Even "select all" context menu was hijacked. So I gave up. I'll have to file an issue.

Re: Functional Programming with TypeScript's Type System

#23
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Yeah, there are some weird stuff in typescript, for instance, this typechecks

    class Animal {}

    class Dog extends Animal {
        woof() {}
    }

    class Cat extends Animal {
        meow() {}
    }

    let append_animals = (animals: Animal[], animal: Animal) => animals.push(animal)

    let dogs = [new Dog()]
    append_animals(dogs, new Cat())

    dogs.map(dog => dog.woof())

Which if you evaluate, you'll obviously get:

    Uncaught TypeError: dog.woof is not a function
Whereas Mypy won't typecheck the equivalent Python code:

    class Animal:
        pass

    class Dog(Animal):
        pass


    def append_animals(animals: List[Animal], animal: Animal) -> None:
        animals.append(animal)


    dogs = [Dog()]
    append_animals(dogs, Dog())

It'll throw with:

    $ mypy types.py 
    types.py:16: error: Argument 1 to "append_animals" has incompatible type "List[Dog]"; expected "List[Animal]"
    types.py:16: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
    types.py:16: note: Consider using "Sequence" instead, which is covariant

Re: Functional Programming with TypeScript's Type System

#24
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Are there other examples of ways TS confounds you?

This feels like a "haha, gotcha!" moment. Like Gary Bernhardt's Wat talk, it's one single example that looks extremely silly... but has next to no actual impact on anyone using the language regularly, is like the faintest little quirk.

Typescript seems reasonably acceptable. I struggle to think of what I would ask for, what would be significantly massively different in my life if there were a hypothetical much better alternative to Typescript.

Re: Functional Programming with TypeScript's Type System

#25
post #24
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Are there other examples of ways TS confounds you? This feels like a "haha, gotcha!" moment. Like Gary Bernhardt's Wat talk, it's one single example that looks extremely silly... but has next to no actual impact on anyone using the language regularly, is like the faintest little quirk. Typescript seems reasonably acceptable. I struggle to think of what I would ask for, what would be significantly massively different…

>> if there were a hypothetical much better alternative to Typescript. Like ReScript ?

Re: Functional Programming with TypeScript's Type System

#26
As wonderfully absurd as this is, I learned more about TypeScript’s type system from this post than I have from its documentation.

Entirely possible that PEBKAC, but I’ve found TypeScript’s documentation to be on the worse end of the programming language documentation quality spectrum.

Re: Functional Programming with TypeScript's Type System

#27
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Yeah, there are some weird stuff in typescript, for instance, this typechecks class Animal {} class Dog extends Animal { woof() {} } class Cat extends Animal { meow() {} } let append_animals = (animals: Animal[], animal: Animal) => animals.push(animal) let dogs = [new Dog()] append_animals(dogs, new Cat()) dogs.map(dog => dog.woof()) Which if you evaluate, you'll obviously get: Uncaught TypeError: dog.woof is not a f…

From my point of view, this is one of those cases where structural typing just doesn't work all that well when used for OOP.

Typescript does give you a solution to this problem, namely that you use generics to constrain the parameters of your method:

    let append_animals = (animals: T[], animal: T) => animals.push(animal)
Your example now gives the expected error.

TypeScript does indeed have its quirks, but most of them do not really matter for real-life purposes or can easily be worked around like in the example above.

Re: Functional Programming with TypeScript's Type System

#28
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Having used TS in production with web and mobile (React Native) apps, most of it is rather simple interfaces to make sure you are passing data correctly. I'd say 99.9% of TS code I saw was to make sure that you passed SuperComplexBusinessObject correctly.

But like you, I've come across many instances where I got "hey TS aren't you supposed throw an error here?"

Re: Functional Programming with TypeScript's Type System

#29
post #26

As wonderfully absurd as this is, I learned more about TypeScript’s type system from this post than I have from its documentation. Entirely possible that PEBKAC, but I’ve found TypeScript’s documentation to be on the worse end of the programming language documentation quality spectrum.

It’s good for reference but not for discovery. If I already know the general concept (let’s say Template Literal Types) I can get good info on it, but if I start with a question like ‘Is there a way to make sure this string literal starts with “id_”?’ then I find it very hard to know.

Random but this is what I’m finding GPT-4 best at: translating random questions into domain terminology + providing examples.

Post reply on HN