javascript - 未提供 HTMLScriptElement 的所有属性时出现 typescript 错误

标签 javascript typescript types

我正在将脚本附加到 typescript 文件中的页面。

function getScript() {
  return new Promise((resolve, reject) => {
    const script: HTMLScriptElement = document.createElement('SCRIPT');
    document.body.appendChild(script);
    script.onload = resolve;
    script.onerror = reject;
    script.async = true;
    script.src = './scripts/my-script.js';
  });
}

Typescript 给我带来了以下错误,我之前在 img 标签上遇到过这种错误,但该错误表明我缺少很多很多属性。

Type 'HTMLElement' is missing the following properties from type 'HTMLScriptElement': async, charset, crossOrigin, defer, and 8 more.ts(2740)

我不想使用/指定脚本元素接口(interface)上可用的所有属性:我应该使用更通用的东西(什么?),还是完全省略接口(interface)?或者有没有一种方法可以使用界面,以便我获得用于自动完成的智能感知帮助程序,如果界面上不存在 Prop ,则会警告我,但当我不使用它们时,会保持沉默?

最佳答案

SCRIPT 更改为 script,TypeScript 将意识到 createElement() 返回一个 HTMLScriptElement

关于javascript - 未提供 HTMLScriptElement 的所有属性时出现 typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57698613/

相关文章:

javascript - 从 highstock 图表工具提示中删除日期名称

javascript - Backbone.js Collection View 渲染重复项

angular - 协助解决 TS2570 错误 : Property 'sendEmailVerification' does not exist on type 'Promise<User>' . 您是否忘记使用 'await' ?

java - 我应该永远不再使用原始类型吗?

javascript - 从 JavaScript 打开/保存本地 (JSON) 文件 >> IE/Firefox

javascript - 有没有办法为 Jasmine 单元测试(Angular 4)提供基类?

typescript - 类型 'Promise<number>' 不可分配给类型 'number'

c# - .Net 中的类型转发 : Does the class forwarded need to inherit from the Type class?

c# - 检查类型最快的方法是什么?

javascript - 获取 Javascript 关联数组的一部分?