此打字稿摘要中的“(this as any)"是什么意思? [英] what does `(this as any)` mean in this typescript snippet?
问题描述
我符合此代码,但不完全了解它的作用:
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屋!