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…
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...