I am trying to create a class or constructor function, which I specify a list of values to be used in the type of one of it's methods.
As an example I have this code.
const MyAnimal = function (animal: string[]) {
type Animal = typeof animal[number]
this.getAnimal = (url: Animal) => {
console.log(url)
}
}
const animalTest = new MyAnimal(['sheep', 'dog', 'cat'])
// I would like this to fail as 'mouse' is not part of the array ['sheep', 'dog', 'cat']
animalTest.getAnimal('mouse')
I want getAnimal to have the type 'sheep' | 'dog' | 'cat' and for the intellisense to warn me if I add something different
Is this possible?