用户定义的类型保护 [打字稿] [英] user defined type guards [typescript]

查看:22
本文介绍了用户定义的类型保护 [打字稿]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 TypeScript 1.6 是否可以编写函数来根据接口检查对象.(以下是MSDN的公告和使用方法.)

我的问题针对返回表达式 return a.name === 'kitty';:

  1. 扮演一个角色?
  2. 所有的猫都必须叫kitty吗?

<块引用>

用户定义的类型保护

[http:///blogs.msdn.com/b/typescript/archive/2015/09/16/annoucing-typescript-1-6.aspx]

在早期版本的 TypeScript 中,您可以使用 if 语句来缩小类型.例如,您可以使用:

if (typeof x === "number") { ... }

这有助于将信息流输入到常用的工作方式中运行时类型(受其他一些项目的启发)JS 的类型检查).虽然这种方法很强大,但我们想要再推一点.在 1.6 中,您现在可以创建自己的类型保护功能:

interface Animal {name: string;}接口猫扩展动物{喵();}function isCat(a: Animal): a is Cat {return a.name === 'kitty';}var x:动物;如果(isCat(x)){x.喵();//好的,x 在这个块中是 Cat}

这不仅允许您使用 typeof 和 instanceof 检查,这需要 JavaScript 理解的类型,但现在你可以工作了具有接口并进行自定义分析.保护功能被表示通过他们的a is X"返回类型,它返回布尔值和信号到编译器,如果现在是预期的类型.

解决方案

考虑你的例子

interface Animal {name: string;} - 一个接口interface Cat extends Animal { - 一个实现喵();}function isCat(a: Animal): a is Cat {return a.name === 'kitty';//您的特殊检查,您可以将其替换为任何其他检查表达式}var x:动物;如果(isCat(x)){x.喵();//好的,x 在这个块中是 Cat}

这个例子表明,现在我们可以使用像 a is Cat 这样的表达式,并且在下面的块中,已经使用了检查的,a 的类型将是.return a.name === 'kitty'; 表达式可以替换为

function isCat(a: Animal): a is Cat {return a[meow"] != undefined;//这也将表明动物是猫}

它会起作用.

你也可以用这个代替

function isCat(a: Animal): a is Cat {返回真;}

我已经准备好了 例子给你,你可以玩一下.

结论:

  1. 表达式 a is Cat 提供与 a instanceof SomeClass 相同的效果,但第一个是可自定义检查而不是最后一个,最后一个不适用于接口.
  2. 您的检查函数可以包含任何代码 - 这是您的选择
  3. 这个未来让我们有可能检查实例是否是接口

更新

  1. 扮演一个角色?
    • 是的,它会播放.a.name === 'kitty' - 这个表达式显示了检查给定动物 IS - Cat 的逻辑,所以,在这里你可以提供任何 boolean 表达式来检查给定的动物是 Cat,即使你可以只返回 truefalse;
  2. 所有的猫都必须叫kitty吗?
    • 没有.它是由你决定.在函数 isCat 中,您可以提供任何逻辑来确保 given 动物的类型为 Cat

With TypeScript 1.6 is it possible to write functions to check objects against interfaces. (Below is the anncouncement from MSDN and how to use it.)

My question targets the return expression return a.name === 'kitty';:

  1. Plays it an role?
  2. Must all cats called kitty?

User defined type guards

[http://blogs.msdn.com/b/typescript/archive/2015/09/16/announcing-typescript-1-6.aspx]

In earlier versions of TypeScript, you could use if statements to narrow the type. For example, you could use:

if (typeof x === "number") { … }

This helped type information flow into common ways of working with types at runtime (inspired by some of the other projects doing typechecking of JS). While this approach is powerful, we wanted to push it a bit further. In 1.6, you can now create your own type guard functions:

interface Animal {name: string; } 
interface Cat extends Animal {
  meow(); 
}

function isCat(a: Animal): a is Cat {
  return a.name === 'kitty'; 
}

var x: Animal;

if(isCat(x)) {
  x.meow(); // OK, x is Cat in this block
}

This allows you to work with not only typeof and instanceof checks, which need a type that JavaScript understands, but now you can work with interfaces and do custom analysis. Guard functions are denoted by their "a is X" return type, which returns boolean and signals to the compiler if what the expected type now is.

解决方案

Consider your example

interface Animal {name: string; } - an interface
interface Cat extends Animal { - an implementation
  meow(); 
}

function isCat(a: Animal): a is Cat {
  return a.name === 'kitty'; // your special checking you can replace it with any other checking expression
}

var x: Animal;

if(isCat(x)) {
  x.meow(); // OK, x is Cat in this block
}

This example show, that now, we can use expression like a is Cat and in the follow block, where checking has been already used, type of a would be Cat. The return a.name === 'kitty'; expression you can replace with

function isCat(a: Animal): a is Cat {
  return a["meow"] != undefined; // it also would be indicate that the animal is Cat
}

and it would be work.

Even you can replace it with this too

function isCat(a: Animal): a is Cat {
      return true;
}

I have prepared example for you, you can play with it.

Conclusions:

  1. Expression a is Cat provide same effect as a instanceof SomeClass, but first one is customizable checking instead of last one, and last one does not work with interfaces.
  2. Your checking function can contain any code - it is your choice
  3. This future give us possibility to check if instance is inctanceof interface

Update

  1. Plays it an role?
    • Yes, it plays. a.name === 'kitty' - this expression show the logic of checking that given animal IS - Cat, so, here you can provide any boolean expression that check that given animal is Cat even you can return just true or false;
  2. Must all cats called kitty?
    • No. It is up to you. In function isCat you can provide any logic that ensure that given animal is of type Cat

这篇关于用户定义的类型保护 [打字稿]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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