我正在将脚本附加到 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/