0

I am new to Typescript and just exploring the object feature of typescript where I am stuck in accessing the object as we do often in JS.

 module Demo{
     var points1: Object = { x:10};
     var z = points1.x;
    }

At static type checking, I am getting the error Error:(3, 21) TS2339: Property 'x' does not exist on type 'Object'.

According to me the compiled JS is correct.

var Demo;
(function (Demo) {
    var points1 = { x: 10 };
    var z = points1.x;
    console.log(points1);
})(Demo || (Demo = {}));

I am feeling it difficult to identify the error. Any help will be appreciated.

aayush
  • 85
  • 1
  • 6
  • Do not use `Object` unless you really mean to. Instead use `any` or the `object` (lower-case 'o') type which as introduced [in typescript 2.1](https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#object-type) – Nitzan Tomer Apr 12 '17 at 19:14
  • I was about to answer you :( - you need to use an interface for that to be valid typescript: [playground example](https://www.typescriptlang.org/play/#src=interface%20IPoints%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%20%20%20%20y%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Amodule%20Demo%20%7B%0D%0A%20%20%20%20var%20points1%3A%20IPoints%20%3D%20%7B%20x%3A%2010%2C%20y%3A%2020%20%7D%3B%0D%0A%20%20%20%20var%20z%3A%20number%20%3D%20points1.x%3B%0D%0A%7D) – balexandre Apr 12 '17 at 19:18
  • @balexandre Thanks, I know the interface works but I am feeling it difficult to find out the reason why the `Object` key-value accessible feature is removed from Typescript. I doubt that the Object in typescript is different from the Object in Javascript. – aayush Apr 13 '17 at 10:39
  • @NitzanTomer Sorry but this http://stackoverflow.com/questions/36607979/how-to-get-around-property-does-not-exist-on-object does not cover my question. I am not using anything like constructor and public keywords. It is very simple implementation of an object, which needs a `Why` answer for not working as expected. – aayush Apr 13 '17 at 10:42
  • You have the same exact problem that is described in that other thread (and the same exact error), regardless of the constructor or other things that are different. The bottom line is: don't use `Object`, use `any`. As it states [in the docs](https://www.typescriptlang.org/docs/handbook/basic-types.html#any): "You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist" – Nitzan Tomer Apr 13 '17 at 14:25
  • @aayush it's pretty easy to see the reason... with `var a: Object = { x: 10 }` how can TypeScript know the variable type of `x` ? you can't write `var a: Object = { x: number = 10 }` you need to be aware that's one of the main reasons to use TypeScript, and for such, TS makes us use interfaces to be aware of the types. TS does not rely on inference, though it can do that some of the times. – balexandre Apr 13 '17 at 14:27

0 Answers0