更新时间:2022-06-02 08:47:19
我从官方复制/粘贴答案github的VS代码: https://github.com/Microsoft/vscode/issues/ 28495#event-1119847030 。
I copy/paste the answer from the official github's VS Code : https://github.com/Microsoft/vscode/issues/28495#event-1119847030.
为我们的JS和TS语言提供支持的TypeScript服务目前无法识别属性在创建对象后添加到对象中。
The TypeScript service that powers our JS and TS language currently cannot recognize properties added to an object after it is created.
***解决方法是定义对象文字中的所有属性:
The best workaround is to define all the properties in the object literal:
var person = {
name: "azeaze",
sayHello: function() {
console.log("Hi! My name is ", this.name)
}
}
或添加一个明确的使用jsdocs输入:
or to add an explicit type using jsdocs:
/**
* @type {{ name: string, sayHello: () => void}}
*/
// @ts-ignore
var person = {}
person.name = "azeaze"
person.sayHello = function() {
console.log("Hi! My name is ", this.name)
}