5 ms·
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
by mdtrooper 7mo ago
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 7mo agoThe 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).
- culi 7mo agoThis does act exactly as a discriminated union. The code works exactly as written.
- zem 7mo agothis 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/ https://viralinstruction.com/posts/uniontypes/
- epolanski 7mo agoYou need a tagged union for this in typescript.
- spankalee 7mo agoYou'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.
- littlecranky67 7mo agoTypeguard 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.
- culi 7mo agoYou 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
- littlecranky67 7mo agoIt depends how you construct Dog and Cat. With Javascripts dynamic prototype chain, you could never know for sure.
- culi 7mo agoTry it https://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcygXigbygIwIYCcDWAXFABQCUKAfFAG5wCWAJlAL4BQokUAwtsCuigBbCHADuxclVoNm7NgDMArgDsAxsHpwVUAM6Rs+EthX0h2ADbF4SAD48+FNGyiuo9BaQBEOAl-c6JmaWTi5u4UHmFgB0vkZkANxh4a4A9KlQAHoA-MmsUBAWutDOKW6RltEi4uRJZWkZOXnsLEA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcyg...
- deleted 7mo ago[deleted]
- reitzensteinm 7mo agotype Mutt = Dog & Cat const 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.
- castral 7mo agoYou 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.
- culi 7mo agoYour first code block works exactly as you would expect and has been working like that for many years https://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcygXigbygIwIYCcDWAXFABQCUKAfFAG5wCWAJlAL4BQokUAwtsCuigBbCHADuxclVoNm7NgDMArgDsAxsHpwVUAM6Rs+EthX0h2ADbF4SAD48+FNGyiuo9BaQBEOAl-c6JmaWTi5u4UHmFgB0vkZkANxh4a4A9KlQAHoA-MmsUBAWutDOKW6RltEi4uRJZWkZOXnsLEA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAIg9gcyg...
- Incipient 7mo agoThe op did say they didn't want to do these type of checks. I thought the answer was 'instanceof'? https://www.typescriptlang.org/docs/handbook/2/narrowing.html#instanceof-narrowing https://www.typescriptlang.org/docs/handbook/2/narrowing.htm...
- culi 7mo agoI see what you mean, thanks. instanceof works if you're using javascript classes but not for "types". You can't do `instanceof Dog`. `instanceof` is a JavaScript feature https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- paulddraper 7mo agoYou'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-manipulation/typescript-discriminated-union https://www.convex.dev/typescript/advanced/type-operators-ma...
- ivanjermakov 7mo agoThat's intentional, TS types are erased in runtime. My go-to way for this is discriminated unions: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions https://www.typescriptlang.org/docs/handbook/unions-and-inte...
- dimava 7mo agoAnd then you have a const CatDog = { bark(){}, meow(){} } and const TreeCat = { bark: "oak", meow(){} } and your code stops working To make types discriminatable you need either type A = { bark: fn, meow?: never } | { bark?: never, meow: fn } type B = { species: "dog", bark: fn } | { species: "cat", meow: fn } or use instanceof with a class
- karmakaze 7mo agoWhat always bothers me with enums, sealed-types, etc is that I can't compose a new ad-hoc set based on elements of someone else's enum. You can make one using the other but not the other way around, TypeScript's is more general.