编译报错 TS2339: Property 'xxxx' does not exist on type 'HTMLElement'
尝试过
let ele: HTMLImageElement = document.getElementById("img") as HTMLImageElement
可是依然报错:
是我的 ts 配置不对吗?
{ "compilerOptions": { "outDir": "./dist/", "sourceMap": true, "lib": ["es5", "es6", "dom"], "alwaysStrict": true, "allowSyntheticDefaultImports": true, "module": "es6", "target": "es5", "jsx": "react", "allowJs": true } }
Package 完整代码:https://github.com/mashirozx/sakura2
![]() | 1 mashirozx OP 标题描述可能不太妥当,应当是 HTML 对象 --题主补充 |
2 easybin 2019-11-02 10:40:43 +08:00 ![]() 你是要用里面的东西吗 直接 xxx['xxx'] |
![]() | 4 maomaomao001 2019-11-02 10:49:24 +08:00 anyscript |
![]() | 5 love 2019-11-02 11:03:46 +08:00 我这里怎么不报错??? const ele = document.querySelector('#cover-img') as HTMLImageElement const eleW: number = ele.naturalWidth console.log(eleW) |
![]() | 6 love 2019-11-02 11:06:14 +08:00 另外二楼的直接['xxx']是个什么鬼,这不是一样的,写起来还麻烦 |
![]() | 7 xxx749 2019-11-02 11:11:40 +08:00 via Android any 大法好 |
![]() | 8 mashirozx OP |
![]() | 9 momocraft 2019-11-02 11:16:57 +08:00 `as unknown as TheType` 你自己知道是对的就行 |