Discussion
Announcing TypeScript 6.0 RC
pscanf: As a user, I'm of course very excited about v7. As a developer of an app that _integrates_ TypeScript, however, I feel a bit uneasy seeing the API feature still marked as "not ready" on the roadmap.On the other hand, I can understand leaving it as the last thing to do, after the foundation has set. Also, the TypeScript team has really done an amazing job all these years with backward compatibility, so that is extremely reassuring.Maybe my uneasiness is just impatience to get to use the sped-up version in my app as well! :)
tmaly: I had no idea they were using Go to write a compiler.
alembic_fumes: > strict is now true by defaultI 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.
369548684892826: Just use deno
Analemma_: 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.
culi: 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
tshaddox: 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.
mdtrooper: 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(); } }
tshaddox: 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).
zem: 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/
chrysoprace: 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.
spankalee: Semantic versions would at least be playing nice with how npm manages version ranges. If every release of TypeScript is breaking, then you should use major versions so that `npm update` doesn't break your project.Yes, typescript would be at version 60 now. No, that's not a problem at all. Numbers are free.
littlecranky67: Typeguard is what you are looking for: function isDog(animal: Dog | Cat): animal is Dog { return "bark" in Dog }Then: isDog(animal) ? animal.bark() : animal.meow() You get full type narrowing inside conditionals using typeguards.
spankalee: As a developer that integrates typescript into client-side web pages and writes lots of custom plugins, I'm extremely nervous about the port to Go.I think the last estimate I saw was the the Go port compiled to WASM was going to be about triple the size of the minified JS bundle.
castral: You can literally do what your generated example does using a type guard. You can also use method overloaded signatures if you dont want to expose your API consumers to union types.
CuriousRose: I am hoping that eventually there will be some agnosticism with the language server. Currently they all seem to rely on Node which in my opinion is rapidly being outclassed by Bun in terms of resource usage. It would be great to eventually be able to pick what runtime they use in VS Code/Cursor to reduce energy drain on my laptop when working on the go.
culi: This does act exactly as a discriminated union. The code works exactly as written.
culi: You don't even need that. The code exactly as presented acts as a discriminator. TypeScript is smart enough to handle that logic in the if block and know whether animal has been validated as Dog vs Cat. GP is complaining about a feature that already exists in TypeScript
culi: Your first code block works exactly as you would expect and has been working like that for many yearshttps://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcyg...
djrenren: The typescript language server (along with the rest of the compiler) is being rewritten in go for Typescript 7. This work has been going on in parallel with the work on 6.0. The go port has most features of 6.0 already and you can follow its progress and read more about it here:https://github.com/microsoft/typescript-go
CuriousRose: Sure, but instead of waiting another year for this to be launched - with a beta version precaution and inevitable bugs - why not remove the limited hard-coded Node paths and references now and ship something 80:20?
littlecranky67: It depends how you construct Dog and Cat. With Javascripts dynamic prototype chain, you could never know for sure.
culi: Try ithttps://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcyg...
paulddraper: You're looking for a discriminated union [1], which idiomatically: type Dog = { bark(): void; type: 'dog' } type Cat = { meow(): void; type: 'cat' } function speak(animal: Dog | Cat) { if (animal.type === 'dog') { animal.bark() } else { animal.meow() } } Generally speaking, TypeScript does not add runtime features.TypeScript checks your use of JavaScript runtime features.[1] https://www.convex.dev/typescript/advanced/type-operators-ma...
djrenren: If it’s a simple change, they might accept a PR. If it isn’t, well that’s your answer. Not worth allocating engineers for it.
ukuina: > Numbers are free.Someone please tell the LLM naming committee.
reitzensteinm: type Mutt = Dog & Catconst imposter: Mutt = { bark: () => console.log("woof"), meow: () => console.log("meow"), }You're both misunderstanding parent's point as well as the original point. Nobody ever claimed your link wouldn't compile.