Live data from Hacker News

TypeScript 6.0 RC

devblogs.microsoft.com

11–20 of 52 posts

Re: TypeScript 6.0 RC

#11

> strict is now true by default I would still have a full head of hair if this had been the case since the beginning. Nonetheless I am glad that we got here in the end.

TypeScript never would’ve taken off if it had been strict from the beginning, it would’ve been just another forgotten gravestone next to Dart and CoffeeScript. I’m not saying those are bad languages, they’re not, but anything other than a very slow and gradual opt-in transition was just a non-starter. It was painful, but TypeScript played the long game.

Re: TypeScript 6.0 RC

#12
What has always bothered me about TypeScript are union types. If you have a function that receives a parameter such as ‘Dog | Cat’, you cannot separate it. For example:

type Dog = { bark: () => void }

type Cat = { meow: () => void }

function speak(animal: Dog | Cat) {

    if (‘bark’ in animal) {

        animal.bark();

    } else {
        animal.meow();
    }
}

Okay, okay, I know you can filter using ‘in’ to see if it has methods, but in real life, in a company where you have a colleague (who is a golden boy) who writes over-engineered code with hundreds of interfaces of interfaces, you don’t want to spend time searching through the files to find every element that is in the union type.

Whereas in Rust it does:

struct Dog { name: String, }

struct Cat { name: String, }

enum Animal {

    Dog(Dog),

    Cat(Cat),
}

fn process_animal(animal: Animal) {

    match animal {

        Animal::Dog(dog) => {

            println!(‘It is a dog named {}’, dog.name);

        }

        Animal::Cat(cat) => {

            println!(‘It is a cat named {}’, cat.name);

        }
    }
}

I think TypeScript should add a couple of lines of code to the generated JavaScript to do something like:

type Dog = { bark: () => void }

type Cat = { meow: () => void }

function speak(animal: Dog | Cat) {

    if (animal is Dog) {

        animal.bark();

    } else {

        animal.meow();

    }
}

Re: TypeScript 6.0 RC

#13
My deepest thanks to one @AlCalzone for stepping up to Opus port Decorators to tsgo. https://github.com/microsoft/typescript-go/pull/2926

It would have been an affront to have a 6.0 that shipped without the means to work for so so many javascript frameworks/libraries. AlCalzone utterly saving the day.

I also am so so so thankful that maybe perhaps after what felt like a never ending dolldrums, we may finally be getting decorator support in some browsers. What a colossal relief it will be to see the progress of the language become manifest.

Re: TypeScript 6.0 RC

#14

What has always bothered me about TypeScript are union types. If you have a function that receives a parameter such as ‘Dog | Cat’, you cannot separate it. For example: type Dog = { bark: () => void } type Cat = { meow: () => void } function speak(animal: Dog | Cat) { if (‘bark’ in animal) { animal.bark(); } else { animal.meow(); } } Okay, okay, I know you can filter using ‘in’ to see if it has methods, but in real l…

The idiomatic way to do this in TypeScript is with discriminated unions. You’re basically just giving the type system an extra property that makes it trivial to infer a type guard (while also making the runtime check in the compiled JavaScript foolproof).

Re: TypeScript 6.0 RC

#15
post #2

For anyone who isn't aware, TypeScript does not use semantic versioning. Though TypeScript 7.0 will be significant in that it will use the new Go compiler

Semantic versioning seems slightly weird to even apply to something like TypeScript. You could have patch releases that fix actual compiler crashes, sure, but what is a minor release?

Surely any new feature that causes code to fail to type check when it previously would pass (or vice versa) would have to be considered a breaking change.

A similar thing applies to code formatting tools like Prettier, or any linter.

Re: TypeScript 6.0 RC

#16

> strict is now true by default I would still have a full head of hair if this had been the case since the beginning. Nonetheless I am glad that we got here in the end.

If it had been, maybe I wouldn't have had to spend years getting buy in for turning on that setting in my team's codebase.

Re: TypeScript 6.0 RC

#17

What has always bothered me about TypeScript are union types. If you have a function that receives a parameter such as ‘Dog | Cat’, you cannot separate it. For example: type Dog = { bark: () => void } type Cat = { meow: () => void } function speak(animal: Dog | Cat) { if (‘bark’ in animal) { animal.bark(); } else { animal.meow(); } } Okay, okay, I know you can filter using ‘in’ to see if it has methods, but in real l…

this post on union types versus sum types is worth a read (the tl;dr is that they both have their uses and one is not strictly better) https://viralinstruction.com/posts/uniontypes/

Re: TypeScript 6.0 RC

#18
post #2

For anyone who isn't aware, TypeScript does not use semantic versioning. Though TypeScript 7.0 will be significant in that it will use the new Go compiler

Appreciate the reminder (the lack of SemVer has thrown me in the past). In this case, 6.0 is a bigger change than normal:

> TypeScript 6.0 arrives as a significant transition release, designed to prepare developers for TypeScript 7.0, the upcoming native port of the TypeScript compiler. While TypeScript 6.0 maintains full compatibility with your existing TypeScript knowledge and continues to be API compatible with TypeScript 5.9, this release introduces a number of breaking changes and deprecations that reflect the evolving JavaScript ecosystem and set the stage for TypeScript 7.0.

Re: TypeScript 6.0 RC

#19

What has always bothered me about TypeScript are union types. If you have a function that receives a parameter such as ‘Dog | Cat’, you cannot separate it. For example: type Dog = { bark: () => void } type Cat = { meow: () => void } function speak(animal: Dog | Cat) { if (‘bark’ in animal) { animal.bark(); } else { animal.meow(); } } Okay, okay, I know you can filter using ‘in’ to see if it has methods, but in real l…

You need a tagged union for this in typescript.

Re: TypeScript 6.0 RC

#20

What has always bothered me about TypeScript are union types. If you have a function that receives a parameter such as ‘Dog | Cat’, you cannot separate it. For example: type Dog = { bark: () => void } type Cat = { meow: () => void } function speak(animal: Dog | Cat) { if (‘bark’ in animal) { animal.bark(); } else { animal.meow(); } } Okay, okay, I know you can filter using ‘in’ to see if it has methods, but in real l…

You're talking about adding a runtime feature. TypeScript doesn't do that anymore. It can't control what properties are on objects or add new ones - you do that yourself in the standard JavaScript portion of the language. TypeScript only lets you describe what's there.

As a sibling said, discriminated unions are they way to go here. You can also add custom type guard functions if you can't control the objects but you want to centralize the detection of the types, but it's better to let TypeScript do it itself so that you don't mess something up with a cast.

Post reply on HN