|
|
|
|
|
by paulddraper
98 days ago
|
|
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... |
|