在 TypeScript 中使用 is 运算符的原因是什么?
What is reason to use is operator in TypeScript?
在这种情况下,在 TypeScript 中使用 is 运算符的原因是什么?
type Species = "cat" | "dog";
interface Pet {
species: Species
}
interface Cat extends Pet {
}
function petIsCat(pet: Pet): pet is Cat {
return pet.species === "cat";
}
如果 body returns bool 类型,为什么要使用 pet is Cat
而不是 boolean
?
boolean 只是一种数据类型,而 'is' 运算符用于 type-testing。让我稍微改变一下你的例子:
type Species = 'cat' | 'dog';
interface Pet {
species: Species;
}
class Cat implements Pet {
public species: Species = 'cat';
public meow(): void {
console.log('Meow');
}
}
function petIsCat(pet: Pet): pet is Cat {
return pet.species === 'cat';
}
function petIsCatBoolean(pet: Pet): boolean {
return pet.species === 'cat';
}
const p: Pet = new Cat();
p.meow(); // ERROR: Property 'meow' does not exist on type 'Pet'.
if (petIsCat(p)) {
p.meow(); // now compiler knows for sure that the variable is of type Cat and it has meow method
}
if (petIsCatBoolean(p)) {
p.meow(); // ERROR: Property 'meow' does not exist on type 'Pet'.
}
当然你可以使用类型转换:
(<Cat>p).meow(); // no error
(p as Cat).meow(); // no error
但要视情况而定
我的理解是这样的:
宠物是猫
- 是函数return类型。
- 因为它具有“变量类型”语法,所以称为用户定义类型检查
- 此函数 return 类型可在编译时由您的代码“type-checker”使用:
所以:
在你的代码编辑器中配备了一些用于 TS 的类型检查软件,它会显示 0 个编译错误并且会在 user-define type guard:
之后知道 person1 的类型
const isPerson = (object: any): object is Person => "address" in object
interface Person {
address: string
}
// type of person1 is {address: string}
const person1 = {
address: "rue"
}
if(isPerson(person1)){
console.log(person1.address) // type is Person as opposed to {address: string}
} else {
console.log("person is not a Person")
}
希望对您有所帮助
在这种情况下,在 TypeScript 中使用 is 运算符的原因是什么?
type Species = "cat" | "dog";
interface Pet {
species: Species
}
interface Cat extends Pet {
}
function petIsCat(pet: Pet): pet is Cat {
return pet.species === "cat";
}
如果 body returns bool 类型,为什么要使用 pet is Cat
而不是 boolean
?
boolean 只是一种数据类型,而 'is' 运算符用于 type-testing。让我稍微改变一下你的例子:
type Species = 'cat' | 'dog';
interface Pet {
species: Species;
}
class Cat implements Pet {
public species: Species = 'cat';
public meow(): void {
console.log('Meow');
}
}
function petIsCat(pet: Pet): pet is Cat {
return pet.species === 'cat';
}
function petIsCatBoolean(pet: Pet): boolean {
return pet.species === 'cat';
}
const p: Pet = new Cat();
p.meow(); // ERROR: Property 'meow' does not exist on type 'Pet'.
if (petIsCat(p)) {
p.meow(); // now compiler knows for sure that the variable is of type Cat and it has meow method
}
if (petIsCatBoolean(p)) {
p.meow(); // ERROR: Property 'meow' does not exist on type 'Pet'.
}
当然你可以使用类型转换:
(<Cat>p).meow(); // no error
(p as Cat).meow(); // no error
但要视情况而定
我的理解是这样的:
宠物是猫
- 是函数return类型。
- 因为它具有“变量类型”语法,所以称为用户定义类型检查
- 此函数 return 类型可在编译时由您的代码“type-checker”使用: 所以: 在你的代码编辑器中配备了一些用于 TS 的类型检查软件,它会显示 0 个编译错误并且会在 user-define type guard: 之后知道 person1 的类型
const isPerson = (object: any): object is Person => "address" in object
interface Person {
address: string
}
// type of person1 is {address: string}
const person1 = {
address: "rue"
}
if(isPerson(person1)){
console.log(person1.address) // type is Person as opposed to {address: string}
} else {
console.log("person is not a Person")
}
希望对您有所帮助