我想在打字稿中使用FormData 。 不幸的是, 生成的打字稿定义文件不支持带有表单元素的FormData构造函数,详见Typescript Issue#1074 。
我有以下代码:
var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = new FormData(formEl);该代码给出了以下错误,因为生成的定义是错误的:
错误TS2346:提供的参数与呼叫目标的任何签名不匹配。
我想要使用下面的声明:
declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; }但是,如果我包含该类型定义,则会出现此错误:
错误TS2403:随后的变量声明必须具有相同的类型。 变量'FormData'必须是'{new():FormData; 原型:FormData; }',但这里有'{new(form ?: HTMLFormElement):FormData; 原型:FormData; }”。
我该如何解决这个问题?
I want to use FormData in typescript. Unfortunately, the generated typescript definition files doesn't support a FormData constructor with a Form Element as detailed in Typescript Issue #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 wrong:
error TS2346: Supplied parameters do not match any signature of call target.
I want to use the following declaration:
declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; }But, if I include that type definition, I get this error:
error TS2403: Subsequent variable declarations must have the same type. Variable 'FormData' must be of type '{ new (): FormData; prototype: FormData; }', but here has type '{ new (form?: HTMLFormElement): FormData; prototype: FormData; }'.
How can I work around this issue?
最满意答案
我该如何解决这个问题?
潜力1:
发送公关。
潜力2:
更新已发布的lib.d.ts :
declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; }潜力3:
复制并自定义lib.d.ts并使用--noLib编译并手动引用您的自定义lib.d.ts
潜力4:
绕过类型检查器
var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = new window['FormData'](formEl);How can I work around this issue?
Potential 1:
Send a PR.
Potential 2:
Update the shipped lib.d.ts in place:
declare var FormData: { prototype: FormData; new (form?: HTMLFormElement): FormData; }Potential 3:
Copy and customize lib.d.ts and compile with --noLib and manually reference your custom lib.d.ts.
Potential 4:
Bypass the type-checker
var formEl = <HTMLFormElement> document.getElementById("myForm"); var formData = new window['FormData'](formEl);更多推荐
发布评论