r/typescript • • Jan 29 '24

Announcing TypeScript 5.4 Beta

https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-beta/
178 Upvotes

30 comments sorted by

56

u/Pevio1024 Jan 29 '24

If those type narrowing changes are as good as they seem, that's a huge improvement and will save a lot of unneeded if statements, ! operators, and more.

25

u/stank453 Jan 29 '24

Excited for Object.groupBy! Now we just need common Set operations and I'll be mostly satisfied with the JS/TS std lib 

16

u/bakkoting Jan 30 '24

Set methods are already shipping in Safari and will be in Chrome in a couple weeks, and I just sent a PR to add types for them to TS.

2

u/NoInkling Jan 31 '24

Iterator helpers will also be coming to Chrome/V8, after the unfortunate delay with the webcompat issue.

0

u/dwelch2344 Jan 30 '24

Not all heroes wear capes

12

u/smthamazing Jan 29 '24

Thank you for the amazing work! You guys are making the web development world a better place.

11

u/johnkapolos Jan 30 '24

Preserved Narrowing in Closures is going to be very helpful, thank you devs.

1

u/incutonez Jan 30 '24

Hello! Am I misunderstanding the narrowing changes here? Shouldn't this example be valid in the 5.4 beta?

2

u/DanielRosenwasser Jan 30 '24

It doesn't work because as soon as startRow is mutated within some closure, TypeScript won't try to preserve any narrowing within any closure at all.

You could argue that in this case specifically, your mutation isn't invalidating the narrowed type, but that would require some more expensive bookkeeping which 5.4 doesn't do.

1

u/incutonez Jan 31 '24

Okay, thank you for the detailed explanation!

7

u/NobleNobbler Jan 29 '24

TypeScript's type system being structural and not nominal makes my C# brain cry

3

u/DanteIsBack Jan 30 '24

What does that mean?

11

u/sharlos Jan 30 '24

In typescript any type is compatible if they have the same structure (properties, etc), many other languages instead require a type to be the same explicit type to be accepted even if they're functionally identical.

1

u/DanteIsBack Jan 30 '24

Got it, thanks!

5

u/johnkapolos Jan 30 '24

We can look down upon you from our High Order Haskell horse, so relax :P

2

u/politerate Jan 30 '24

Both have pros and cons

2

u/traintocode Jan 30 '24

What are the cons of structural typing?

7

u/politerate Jan 30 '24

Well, you run into the problem of having similar structures which should represent different things. E.g. type Price = { value: number, currency: string } and type Debt = { value: number, currency: string }. Price extends Debt, even when it should not

1

u/Bloompire Jan 31 '24

Also, structural typing is not friendly for "performance-first languages". In JS everything is dynamic, but in C++ , C# or Java its not and function that accepts some type as parameter is compiled for exactly that type, everything is more hardcoded in that type of languages.

If you have a method that accepts Person type as argument and you want to modify 2nd field "age" to a value of 10 for of that object, its compiled as "write value 10 at byte x+4" for example. And different types might have different memory layouts.

1

u/Hurinfan Jan 30 '24

You can make it nominal with hacks but yeah, not ideal.

1

u/dgreensp Jan 30 '24

More goodies than usual in this version!

There has been a hacky user-space NoInfer utility type (or two) floating around for years, which I’ve used before in rare situations, particularly with a certain library I wrote once. 2019 me is in shock that this is just, like, a built-in thing now and you don’t have to define your own NoInfer!

-2

u/Comfortable-Ask8525 Jan 30 '24

eslint doesn't like param reassigning

11

u/AndrewGreenh Jan 30 '24

Eslint doesn’t like what you tell it to not like :p

-51

u/avoere Jan 29 '24

Boring

17

u/Pavlo100 Jan 29 '24

It might seem that way, but the amount of work that has been put into improving the editor experience as of late is amazing.

You used to have to tell TypeScript what type you were working with.

Nowadays, TypeScript tells you which type you are working with.