打字稿。是什么意思:(这和任何一样) [英] typescript. what is mean: (this as any)

查看:312
本文介绍了打字稿。是什么意思:(这和任何一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这段代码并且不明白它的作用:

I meet this code and do not understand exactly what it does :

public uploadItem(value:FileItem):void {
    let index = this.getIndexOfItem(value);
    let item = this.queue[index];
    let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
    item._prepareToUploading();
    if (this.isUploading) {
      return;
    }
    this.isUploading = true;
    (this as any)[transport](item);
  }

任何人都可以解释这个(这个是什么)声明吗?

Can anyone explain what does this (this as any) statement do?

推荐答案

(这与任何一样)只是 类型断言 ,适用于开发/编译时间并且对运行时没有副作用,因为它纯粹是一个Typescript的东西。如果某些内容与这个相关,比如这个[what] 会输出错误,因为这个错误可以被抑制与(此为任何)[无论如何]

(this as any ) is just a Type Assertion that works on dev/compiling time and has no side effects on run time because it is purely a Typescript thing. It can be useful if something related to this like this[whatever] which outputs an error because this error can be suppressed with (this as any)[whatever]

同时 (这与任何一样)相当于(< any> this)

请注意: - suppressImplicitAnyIndexErrors 作为编译器选项可以抑制这些可能的错误。

Note to mention: --suppressImplicitAnyIndexErrors as a compiler option suppresses those kind of possible errors.

这篇关于打字稿。是什么意思:(这和任何一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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