TypeScript 字符串文字类型
示例
字符串文字类型允许您指定字符串可以具有的确切值。
let myFavoritePet: "dog"; myFavoritePet = "dog";
其他任何字符串都会产生错误。
// Error: Type '"rock"' is not assignable to type '"dog"'. // myFavoritePet = "rock";
与类型别名和联合类型一起,您会得到类似枚举的行为。
type Species = "cat" | "dog" | "bird";
function buyPet(pet: Species, name: string) : Pet { /*...*/ }
buyPet(myFavoritePet /* "dog" as defined above */, "Rocky");
// Error: Argument of type '"rock"' is not assignable to parameter of type "'cat' | "dog" | "bird". Type '"rock"' is not assignable to type '"bird"'.
// buyPet("rock", "Rocky");字符串文字类型可用于区分重载。
function buyPet(pet: Species, name: string) : Pet;
function buyPet(pet: "cat", name: string): Cat;
function buyPet(pet: "dog", name: string): Dog;
function buyPet(pet: "bird", name: string): Bird;
function buyPet(pet: Species, name: string) : Pet { /*...*/ }
let dog = buyPet(myFavoritePet /* "dog" as defined above */, "Rocky");
//狗来自“狗”类型(狗:“狗”)它们适用于用户定义的类型防护。
interface Pet {
species: Species;
eat();
sleep();
}
interface Cat extends Pet {
species: "cat";
}
interface Bird extends Pet {
species: "bird";
sing();
}
function petIsCat(pet: Pet): pet is Cat {
returnpet.species=== "cat";
}
function petIsBird(pet: Pet): pet is Bird {
returnpet.species=== "bird";
}
function playWithPet(pet: Pet){
if(petIsCat(pet)) {
//pet现在来自Cat类型(pet:Cat)
pet.eat();
pet.sleep();
} else if(petIsBird(pet)) {
//宠物现在来自鸟类类型(宠物:鸟类)
pet.eat();
pet.sing();
pet.sleep();
}
}完整的示例代码
let myFavoritePet: "dog";
myFavoritePet = "dog";
// Error: Type '"rock"' is not assignable to type '"dog"'.
// myFavoritePet = "rock";
type Species = "cat" | "dog" | "bird";
interface Pet {
species: Species;
name: string;
eat();
walk();
sleep();
}
interface Cat extends Pet {
species: "cat";
}
interface Dog extends Pet {
species: "dog";
}
interface Bird extends Pet {
species: "bird";
sing();
}
// Error: Interface 'Rock' incorrectly extends interface 'Pet'. Types of property 'species' are incompatible. Type '"rock"' is not assignable to type '"cat" | "dog" | "bird"'. Type '"rock"' is not assignable to type '"bird"'.
//界面Rock扩展Pet{
// type: "rock";
//}
function buyPet(pet: Species, name: string) : Pet;
function buyPet(pet: "cat", name: string): Cat;
function buyPet(pet: "dog", name: string): Dog;
function buyPet(pet: "bird", name: string): Bird;
function buyPet(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.`);
}
} as Cat;
} else if(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.`);
}
} as Dog;
} else if(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.`);
}
} as Bird;
} else {
throw `Sorry we don't have a ${pet}. Would you like to buy a dog?`;
}
}
function petIsCat(pet: Pet): pet is Cat {
returnpet.species=== "cat";
}
function petIsDog(pet: Pet): pet is Dog {
returnpet.species=== "dog";
}
function petIsBird(pet: Pet): pet is Bird {
returnpet.species=== "bird";
}
function playWithPet(pet: Pet) {
console.log(`Hey ${pet.name}, let's play.`);
if(petIsCat(pet)) {
//pet现在来自Cat类型(pet:Cat)
pet.eat();
pet.sleep();
// Error: Type '"bird"' is not assignable to type '"cat"'.
//pet.type= "bird";
//错误:类型“猫”上不存在属性“唱歌”。
//pet.sing();
} else if(petIsDog(pet)) {
//pet现在来自Dog类型(pet:Dog)
pet.eat();
pet.walk();
pet.sleep();
} else if(petIsBird(pet)) {
//宠物现在来自鸟类类型(宠物:鸟类)
pet.eat();
pet.sing();
pet.sleep();
} else {
throw "An unknown pet. Did you buy a rock?";
}
}
let dog = buyPet(myFavoritePet /* "dog" as defined above */, "Rocky");
//狗来自“狗”类型(狗:“狗”)
// Error: Argument of type '"rock"' is not assignable to parameter of type "'cat' | "dog" | "bird". Type '"rock"' is not assignable to type '"bird"'.
// buyPet("rock", "Rocky");
playWithPet(dog);
//输出:嗨,洛基,让我们玩。
//岩石吃。
//岩石漫步。
//岩石睡觉。