Hacker News new | past | comments | ask | show | jobs | submit login
TypeScript 4.4 Beta (microsoft.com)
63 points by bpierre 9 days ago | hide | past | favorite | 20 comments

I'm very happy to see the inclusion of "Control Flow Analysis of Aliased Conditions." This has been something I have been wishing for for awhile now and a delight to see it come out in this release. TypeScript just keeps getting better and better.

Me too. Especially no longer having to explain to TS in many places that "yes, I already checked this type!"

EDIT: What I'd like to see now is a "cleanup" hint, which says: hey I already know what type this is, you can remove these 10 lines of aliasing code in this function.

Interesting, is this a new-ish idea in type systems? The first time I saw something similar was in Kotlin as a technique for dealing with null checks without having to rename the unwrapped variable. Are there other languages that have this feature?

I first saw it in Typed Racket under the name "Occurrence typing" [1]. It's not a coincidence that Typed Racket is also a type system applied retroactively to a dynamically typed language. The first mention of "occurrence typing" in scheme I can find is a 2008 paper [2]

[1] https://docs.racket-lang.org/ts-guide/occurrence-typing.html

[2] https://www2.ccs.neu.edu/racket/pubs/popl08-thf.pdf

TypeScript doesn't do proper occurrence typing:


Compare to Typed Racket:

    #lang typed/racket
    (: foo (-> (U String Null) (U String Null) String))
    (define (foo a b)
      (cond [(and (null? a) (null? b)) "default"]
            [(not (null? a)) a]
            [#t b]))
Or Psalm, a PHP typechecker I created with occurrence typing:


I think that Python's type checkers do this sort of thing now, e.g. via TypeGuard (https://www.python.org/dev/peps/pep-0647/)

That's a PEP for Python 3.10, which isn't even released yet. And Mypy only supports it since May: https://github.com/python/mypy/issues/5206#issuecomment-8441...

I agree this is a huge improvement. It addresses one of the few remaining cases where the anti-TS crowd could rightly claim embracing the type system leads to more convoluted code than JS.

From time to time I come across amazing things made with seemingly advanced type system of typescript (eg. sql engine). What is the trail I should follow if I want learn how to do such things?

Does TypeScript have the most complex and advanced type system ever?

"Advanced" is debatable, but yes to "complex".

At least, in the core language itself, not counting extensions (otherwise Haskell).


I will say that it is a pleasant surprise how flexible and practical TypeScript's type system is.

It would seem that the pressure of working for existing untyped programming patterns has produced something that is very usable and unobtrusive.

TS is the most ergonomic and flexible type system i worked with. It has some deep quirks, but end-user app developers rarely have to do anything with those.

Miss it a lot when i have to push some python or golang code.

I've read a lot online that Scala is complex, but I had an easier time with learning Scala than I had reading through the TypeScript docs. And I dived into TS after Scala.

Except for implicits ha.

It depends how you interpret these things. On a technical level, TS is not sound (you can have runtime type errors in programs that have "correct" types according to TS). For type system aficionados that may not even count as a proper type system.

Using something like TypeBox [1] can help with that.


Tons of programming languages strip the types for runtime. Notably java.

You're thinking of type erasure. That's very different from soundness.

(but is in fact correct that Java's type system isn't entirely sound, I forget the details, but I believe array element type variance is one of the soundness hole.)

This is a compound question, since complexity and advancement are two separate issues.

No, Scala does.

Applications are open for YC Winter 2022

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact