admin管理员组文章数量:1178553
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?
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?
Share Improve this question edited Dec 30, 2019 at 19:15 Sinan 11.6k7 gold badges38 silver badges48 bronze badges asked Mar 2, 2017 at 9:37 madjardimadjardi 5,9292 gold badges38 silver badges40 bronze badges 4 |2 Answers
Reset to default 29(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]
Also (this as any)
is the equivalent to (<any> this)
Note to mention: --suppressImplicitAnyIndexErrors
as a compiler option suppresses those kind of possible errors.
It can be actually written as
(<any>this)[transport](item);
The type casting is exhibited in the above statement!
本文标签: javascriptwhat does (this as any) mean in this typescript snippetStack Overflow
版权声明:本文标题:javascript - what does `(this as any)` mean in this typescript snippet? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738004931a2047895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this as any
is a cast. It casts thethis
to theany
type (which also removes most compile time type checks) – UnholySheep Commented Mar 2, 2017 at 9:46