我遇到了这段代码,但不明白它到底做了什么:
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);
}
谁能解释这个(this as any)语句的作用?
最佳答案
(this as any) 只是一个 Type Assertion 它适用于开发/编译时间并且对运行时间没有副作用,因为它纯粹是一个 Typescript 的东西。如果与 this
相关,它会很有用喜欢this[whatever]
输出 TS 错误因为 whatever
未在 this
内定义TS 类型。所以,这个错误可以用 (this as any)[whatever]
来抑制。
还有 (this as any)
相当于(<any> this)
注意事项: --suppressImplicitAnyIndexErrors
作为compiler option抑制那些可能的错误。
关于javascript - `(this as any)` 在此 typescript 片段中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551681/