此打字稿摘要中的“(this as any)"是什么意思? [英] what does `(this as any)` mean in this typescript snippet?

查看:38
本文介绍了此打字稿摘要中的“(this as any)"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我符合此代码,但不完全了解它的作用:

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?

推荐答案

(任意)只是

(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 a TS error because whatever is not defined inside the this TS type. So, this error can be suppressed with (this as any)[whatever]

(此为任意)等效于(< any>此)

注意事项: -suppressImplicitAnyIndexErrors 作为

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

这篇关于此打字稿摘要中的“(this as any)"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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