I have a typeguard which take a string and I wanna know if it's part of a union type but if we add new string in the union type, I don't want to manage my typeguard by also adding the new string:
type GreatAnimal = 'Dog' | 'Cat'
function isGreatAnimal(pet: string): pet is GreatAnimal {
return pet === 'Dog' || pet === 'Cat'; // Here I would prefer to do something like 'pet keyof GreatAnimal'
}
function Foo(animal:string){
if (isGreatAnimal(animal)) {
// do something
}
else {
// do something else
}
}
If I add Fish in the union type 'GreatAnimal', I don't want to have to update my type guard to manage Fish.
Im using last typescript version