type Animal = { name: string } // received from external source
const cat: Animal = {
name: 'Cat' as const
}
const dog = {
name: 'Dog' as const
}
type CatName = typeof cat.name // = string, not 'Cat' !!!
type DogName = typeof dog.name // = 'Dog'
Can you help me modify the code above to achieve a literal type CatName = 'Cat'
(similar to DogName = 'Dog'
)?
Please keep in mind that I cannot alter the definition of type Animal
since it is part of an external library, and I also want to ensure that the value cat
remains typed.