`is` 关键字在打字稿中有什么作用? [英] What does the `is` keyword do in typescript?

查看:27
本文介绍了`is` 关键字在打字稿中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些看起来像这样的代码:

I came across some code that looks like this:

export function foo(arg: string): arg is MyType {
    return ...
}

我无法在文档或谷歌中搜索 is,这是一个非常常见的词,基本上出现在每个页面上.

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.

关键字在这种情况下有什么作用?

What does the keyword do in that context?

推荐答案

参见 用户定义的类型保护函数了解更多信息.

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");

在上述格式中使用类型谓词test is string(而不是仅使用boolean作为返回类型),在isString() 被调用,如果函数返回 trueTypeScript 会将类型缩小到 string 由函数调用保护的任何块中.编译器会认为 foo 是下保护块中的 string(并且仅在下保护块中)

Using the type predicate test is string in the above format (instead of just using 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)

{
    console.log("it is a string" + foo);
    console.log(foo.length); // string function
}

类型谓词仅在编译时使用.生成的 .js 文件(运行时)将没有区别,因为它不考虑 TYPE.

A type predicate is just used in compile time. The resulting .js file (runtime) will have no difference because it does not consider the TYPE.

我将在以下四个示例中说明差异.

I will illustrate the differences in below four examples.

例如 1:上面的示例代码不会有编译错误或运行时错误.

E.g 1: the above example code will not have a compile error nor a runtime error.

例如2:下面的示例代码将出现编译错误(以及运行时错误),因为 TypeScript 已将类型缩小到 string 并检查 toExponential 不属于 字符串方法.

E.g 2: the below example code will have a compile error (as well as a runtime error) because TypeScript has narrowed the type to string and checked that toExponential does not belong to string method.

function example(foo: any){
    if(isString(foo)){
        console.log("it is a string" + foo);
        console.log(foo.length);
        console.log(foo.toExponential(2));
    }
}

例如3:下面的示例代码没有编译错误,但会出现运行时错误,因为 TypeScript 只会将类型缩小到 string 在受保护的块中而不是之后,因此 foo.toExponential 不会产生编译错误(TypeScript 认为它不是 string 类型).但是在运行时,string没有toExponential方法,所以会出现运行时错误.

E.g. 3: the below example code does not have a compile error but will have a 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.

function example(foo: any){
    if(isString(foo)){
        console.log("it is a string" + foo);
        console.log(foo.length);
    }
    console.log(foo.toExponential(2));
}

例如4:如果我们不使用test is string(类型谓词),TypeScript 不会缩小受保护块中的类型,下面的示例代码不会有编译错误,但会出现运行时错误.>

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.

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));
    }
}

结论是test is string(类型谓词)在编译时用来告诉开发者代码有机会出现运行时错误.对于 javascript,开发人员在编译时不会知道错误.这就是使用 TypeScript 的优势.

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.

这篇关于`is` 关键字在打字稿中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆