typescript `is` 关键字在打字稿中有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40081332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the `is` keyword do in typescript?
提问by Drew
I came across some code that looks like this:
我遇到了一些看起来像这样的代码:
export function foo(arg: string): arg is MyType {
return ...
}
I haven't been able to search for is
in either the docs or google, it's a pretty common word and shows up on basically every page.
我无法is
在文档或谷歌中搜索,这是一个非常常见的词,基本上出现在每个页面上。
What does the keyword do in that context?
关键字在这种情况下有什么作用?
回答by ARIES CHUI
function isString(test: any): test is string{
return typeof test === "string";
}
function example(foo: any){
if(isString(foo)){
console.log("it is a string" + foo);
console.log(foo.length); // string function
}
}
example("hello world");
Using the type predicate "test is string" in the above format (instead of just use boolean for the return type, after isString() is called, if the function returns true, TypeScript will narrow the type to string in any block guarded by a call to the function.The compiler will think that foo is string in the below-guarded block (and ONLY in the below-guarded block)
使用上述格式中的类型谓词“test is string”(而不是仅使用布尔值作为返回类型,在调用 isString() 之后,如果函数返回 true,TypeScript 会将类型缩小到由 a 保护的任何块中的字符串)调用函数。编译器会认为 foo 是低于保护块中的字符串(并且仅在低于保护块中)
{
console.log("it is a string" + foo);
console.log(foo.length); // string function
}
Type predicate is just used in compile time. The result .js file (runtime) will have no difference because it does not consider the TYPE.
类型谓词仅在编译时使用。结果 .js 文件(运行时)将没有区别,因为它不考虑 TYPE。
I will illustrate the differences in below four examples.
我将在以下四个示例中说明差异。
E.g 1: the above example code will not have compile error and runtime error.
例如 1:上面的示例代码不会有编译错误和运行时错误。
E.g 2: the below example code will have compile error (as well as runtime error) because TypeScript has narrowed the type to string and checked that toExponential does not belong to string method.
例如 2:下面的示例代码将有编译错误(以及运行时错误),因为 TypeScript 已将类型缩小为字符串并检查 toExponential 不属于字符串方法。
function example(foo: any){
if(isString(foo)){
console.log("it is a string" + foo);
console.log(foo.length);
console.log(foo.toExponential(2));
}
}
E.g. 3: the below example code does not have compile error but will have runtime error because TypeScript will ONLY narrow the type to string in the block guarded but not after, therefore foo.toExponential will not create compile error (TypeScript does not think it is a string type). However, in runtime, string does not have the toExponential method, so it will have runtime error.
例如 3:下面的示例代码没有编译错误,但会出现运行时错误,因为 TypeScript 只会将类型缩小到受保护的块中的字符串而不是之后,因此 foo.toExponential 不会创建编译错误(TypeScript 认为不是字符串类型)。但是,在运行时,string 没有 toExponential 方法,因此会出现运行时错误。
function example(foo: any){
if(isString(foo)){
console.log("it is a string" + foo);
console.log(foo.length);
}
console.log(foo.toExponential(2));
}
E.g. 4: if we don't use "test is string" (type predicate), TypeScript will not narrow the type in the block guarded and the below example code will not have compile error but it will have runtime error.
例如 4:如果我们不使用“test is string”(类型谓词),TypeScript 不会缩小受保护块中的类型,下面的示例代码不会有编译错误,但会出现运行时错误。
function isString(test: any): boolean{
return typeof test === "string";
}
function example(foo: any){
if(isString(foo)){
console.log("it is a string" + foo);
console.log(foo.length);
console.log(foo.toExponential(2));
}
}
The conclusion is that "test is string" (type predicate) is used in compile-time to tell the developers the code will have a chance to have a runtime error. For javascript, the developers will not KNOW the error in compile time. This is the advantage of using TypeScript.
结论是在编译时使用“test is string”(类型谓词)来告诉开发人员代码将有机会出现运行时错误。对于 javascript,开发人员在编译时不会知道错误。这是使用 TypeScript 的优势。