Typescript which means: (it's like anyone)

I meet this code and don’t understand 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 someone explain what this operator does (it's like anyone else ) ?

+12
source share
2 answers

(like any other) is just a type statement that works during development / compilation and has no side effects at runtime, because it's just a Typescript thing. This can be useful if something related to this for example, this[whatever] throws a TS error, therefore whatever not defined inside this type TS. Thus, this error can be suppressed using (this as any)[whatever]

In addition (this as any) is equivalent to (<any> this)

Note to mention: --suppressImplicitAnyIndexErrors as a compiler option suppresses such possible errors.

+10
source

In fact, it can be written as

  (<any>this)[transport](item); 

Typical casting is presented in the above statement!

+4
source

Source: https://habr.com/ru/post/1015395/


All Articles