Typescript: work on type definition error for inline javascript

I want to use FormData in typescript. Unfortunately, the generated typescript definition files do not support the FormData constructor with the form element, as described in Typescript Issue No. 1074 .

I have the following code:

var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = new FormData(formEl); 

The code gives the following error because the generated definition is incorrect:

error TS2346: The supplied parameters do not match any signature of the target call.

I want to use the following declaration:

 declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; } 

But if I include this type definition, I get this error:

error TS2403: Subsequent variable declarations must be of the same type. The variable "FormData" must be of type "{new (): FormData; prototype: FormData;} ', but there is a type of' {new (form ?: HTMLFormElement): FormData; prototype: FormData;}".

How can I get around this problem?

+5
source share
3 answers

How can I get around this problem?

Potential 1:

Send PR.

Potential 2:

Update sent lib.d.ts in place:

 declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; } 

Potential 3:

Copy and configure lib.d.ts and compile with --noLib and manually specify your own lib.d.ts

Potential 4:

Type Check Bypass

 var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = new window['FormData'](formEl); 
+3
source

There is an error in VS2017 typescript libraries: this can be fixed in the April 2017 update. You can report an error you noted by disabling LanguageService in Tools | Options | Text editor | JavaScript / TypeScript | LanguageService Just uncheck the box next to β€œEnable new JavaScript language service.” checkbox.

More information about the problem https://developercommunity.visualstudio.com/content/problem/25310/unload-projects-hangs-on-close-solution.html

+1
source

There is already an error on GitHub . It is designed to be fixed in TypeScript 1.6.

Until then, this is an easy workaround in TypeScript:

 var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = <FormData> new (<any> FormData) (formEl); 

And the generated javascript is exactly what you wanted:

 var formData = new FormData(formEl); 

This should continue even after fixing the lib.d.ts problem.

0
source

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


All Articles