wangdoc / typescript-tutorial

TypeScript 教程
https://wangdoc.com/typescript
2.43k stars 256 forks source link

第 10 章 关于 interface 章节,接口合并小节中,对 document 添加自定义属性代码片段,编译不通过。 #95

Closed AnglusWang closed 9 months ago

AnglusWang commented 9 months ago

举例来说,Web 网页开发经常会对windows对象和document对象添加自定义属性,但是 TypeScript 会报错,因为原始定义没有这些属性。解决方法就是把自定义属性写成 interface,合并进原始定义。

document.foo = 'hello';

* 编译报错信息

error TS2339: Property 'foo' does not exist on type 'Document'.

ruanyf commented 9 months ago

官方 playground 正常