Typescript: typescript operator '+=' cannot be applied to types Number and Number

Created on 9 Jun 2015  路  2Comments  路  Source: microsoft/TypeScript

Why?

ref: [
  {
    value: Number
    date?: Date
  }
]
total() {
  var total = 0
  for (var ind = this.ref.length - 1; ind >= 0; ind--) {
    total += this.ref[ind].value
  }
  return total
}

This is totaly vaild in JS, should be in typescript too

By Design Canonical

Most helpful comment

You want number (all lowercase) not Number. number is the primitive type, whereas Number is a description of the shape of instances of Number and number itself.

See a more detailed response here: https://github.com/Microsoft/TypeScript/issues/2624#issuecomment-89914698

Or @danquirk's response here: https://github.com/Microsoft/TypeScript/issues/2031#issuecomment-74329927

All 2 comments

You want number (all lowercase) not Number. number is the primitive type, whereas Number is a description of the shape of instances of Number and number itself.

See a more detailed response here: https://github.com/Microsoft/TypeScript/issues/2624#issuecomment-89914698

Or @danquirk's response here: https://github.com/Microsoft/TypeScript/issues/2031#issuecomment-74329927

Note that #2361 aims to address this.

Was this page helpful?
0 / 5 - 0 ratings