Typescript property does not exist on type {}

前端 未结 4 1822
我寻月下人不归
我寻月下人不归 2020-12-02 01:00

I have the following code in Typescript. Why does the compiler throws an error?

var object = {};
Object.defineProperty(object, \'first\', {
         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 01:23

    In the given case, I would just rewrite it as

    var object = {};
    
    var withFirst =  {...object, get first() {return 37;}};
    
    console.log('first property: ' + withFirst.first);
    

提交回复
热议问题