最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - add new property in Typescript object - Stack Overflow

programmeradmin2浏览0评论

I'm trying to add new property in object, but typescript given error.

error TS2339: Property 'qty' does not exist on type 'Object'.

product: Object = {qty: Number};

foo(){
  this.product.qty = 1;
}

I'm trying to add new property in object, but typescript given error.

error TS2339: Property 'qty' does not exist on type 'Object'.

product: Object = {qty: Number};

foo(){
  this.product.qty = 1;
}
Share Improve this question asked Jun 27, 2018 at 5:52 Santosh ShelkeSantosh Shelke 5822 gold badges6 silver badges19 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 3

Object is the wrong annotation. Change your annotation:

product: {qty: number} = {qty: 0};

foo(){
  this.product.qty = 1;
}

Number is a data type not a value. the object only accept the value not datatype. So you can't declare number as a value

try this simple way

product: any;

foo(){
  this.product.qty = 1;
}

Seems that you've tried to do this:

product: {qty: number} = {};

foo(){
  this.product.qty = 1;
}

This won't work, although, because the object you've started with is incomplete. You should either mark the property as optional:

product: {qty?: number} = {};

foo(){
  this.product.qty = 1;
}

Or provide some default value:

product: {qty: number | null} = {qty: null};
// or just {qty: number}, if you don't use strict null checks

foo(){
  this.product.qty = 1;
}

If default value is a number too, you can even simplify this by using type inference:

product = {qty: 0};

foo(){
  this.product.qty = 1;
}

All these three examples will work (if you provide proper value for this in function foo, of course, but I assume that you'll do it anyway).

Use this

product = {qty: null};

OR

product = {qty: 0};

instead of this -

product: Object = {qty: Number};

Because you initializing your object with key and value as type which is number so instead of assigning type you need to set some value which is anything say 0 or null or anything you want as the default value.

You can also do like this and add as many as new properties you want in a product

product: Object = new Object();

foo(){
  this.product['qty'] = 1;
}

A solution without extra code would be a config option to allow the creation of new properties with the assignment operator but since there is no such property i patched the typescript code to allow it.

Do this on both tsc.js and typescript.js, those files are in node_modules/typescript

Find this line

 if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) 

And paste this right under it

return getTypeOfSymbol(right);

I have to admit that im not a typescript src code developer and i havent tested extensively the side effects of doing this but it works for my projects

The linter will show an error but it will compile fine

Change Object to any type

product: any = {qty: Number};

foo(){
  this.product.qty = 1;
}
发布评论

评论列表(0)

  1. 暂无评论