Typescript:解决javascript内置的类型定义错误(Typescript: Work around type definition error for javascript builti

编程入门 行业动态 更新时间:2024-10-25 22:24:19
Typescript:解决javascript内置的类型定义错误(Typescript: Work around type definition error for javascript builtin)

我想在打字稿中使用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);

更多推荐

本文发布于:2023-07-31 19:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   错误   类型   javascript   Typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!