You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
String literal types allow you to specify the exact value a string can have.
letmyFavoritePet: "dog";// console.log(myFavoritePet); // undefined/*myFavoritePet = "rock";console.log(myFavoritePet); // Error: Type '"rock"' is not assignable to type '"dog"'.*/myFavoritePet="dog";//console.log(myFavoritePet); // dog// Together with Type Aliases and Union Types you get a enum-like behavior.typeSpecies="cat"|"dog"|"bird";// They works well for User-Defined Type Guards.interfacePet{species: Species;name: String;eat();sleep();}interfaceCatextendsPet{species: "cat";}interfaceDogextendsPet{species: "dog";walk();}interfaceBirdextendsPet{species: "bird";sing();}/*Error: Interface 'Rock' incorrectly extends interface 'Pet'. Types of property 'species' areincompatible. Type '"rock"' is not assignable to type '"cat" | "dog" | "bird"'. Type '"rock"' is notassignable to type '"bird"'.interface Rock extends Pet { type: "rock";}*/// String Literal Types can be used to distinguish overloads.functionbuyPet(pet :Species,name: String) : Pet;functionbuyPet(pet :"cat",name: String) : Cat;functionbuyPet(pet :"dog",name: String) : Dog;functionbuyPet(pet :"bird",name: String) : Bird;functionbuyPet(pet :Species,name: String) : Pet{if(pet==="cat"){return{species: "cat",name: name,eat: function(){console.log(`${this.name} eats.`);},walk: function(){console.log(`${this.name} walks.`);},sleep: function(){console.log(`${this.name} sleeps.`);}}asCat;}elseif(pet==="dog"){return{species: "dog",name: name,eat: function(){console.log(`${this.name} eats.`);},walk: function(){console.log(`${this.name} walks.`);},sleep: function(){console.log(`${this.name} sleeps.`);}}asDog;}elseif(pet==="bird"){return{species: "bird",name: name,eat: function(){console.log(`${this.name} eats.`);},walk: function(){console.log(`${this.name} walks.`);},sleep: function(){console.log(`${this.name} sleeps.`);},sing: function(){console.log(`${this.name} sings.`);}}asBird;}else{throw`Sorry we do not have a ${pet}. Would you like to buy a dog?`;}};functionpetIsCat(pet: Pet): pet is Cat{returnpet.species==="cat";}functionpetIsDog(pet: Pet): pet is Dog{returnpet.species==="dog";}functionpetIsBird(pet: Pet): pet is Bird{returnpet.species==="bird";}functionplayWithPet(pet: Pet){console.log(`Hey ${pet.name}, lets play.`);if(petIsCat(pet)){// pet is now from type Cat (pet: Cat)pet.eat();pet.sleep();/* // Error: Type '"bird"' is not assignable to type '"cat"'. // pet.type = "bird"; // Error: Property 'sing' does not exist on type 'Cat'. // pet.sing(); */}elseif(petIsDog(pet)){// pet is now from type Dog (pet: Dog)pet.eat();pet.walk();pet.sleep();}elseif(petIsBird(pet)){// pet is now from type Bird (pet: Bird)pet.eat();pet.sing();pet.sleep();}else{throw"An unknown pet. Did you buy a rock?";}}letdog=buyPet(myFavoritePet/* "dog" as defined above */,"Dog-Rocky");letbird=buyPet(myFavoritePet/* "bird" as defined above */,"Bird-Rocky");letcat=buyPet(myFavoritePet/* "cat" as defined above */,"Cat-Rocky");playWithPet(dog);console.log('-----------------\n');playWithPet(bird);console.log('-----------------\n');playWithPet(cat);console.log('-----------------\n');/*Hey Dog-Rocky, lets play.Dog-Rocky eats.Dog-Rocky walks.Dog-Rocky sleeps.-----------------Hey Bird-Rocky, lets play.Bird-Rocky eats.Bird-Rocky walks.Bird-Rocky sleeps.-----------------Hey Cat-Rocky, lets play.Cat-Rocky eats.Cat-Rocky walks.Cat-Rocky sleeps.-----------------*/