0

It adds [x: string]: any; to any object. Why? There is no x property on objects...

VSCode popup helper

This 2nd example demonstrates a problem - when I hover over polygon binding name in polygon.y += 5 expression, IntelliSense shows me a popup where [x: string]: any; is located inside curly braces, so it's very easy to assume that polygon object has x property, doesn't it? Don't you agree that IntelliSense instead should display that [x: string]: any thing above object definition?

2nd example

yaru
  • 1,260
  • 1
  • 13
  • 29
  • 1
    It's just a `type` suggestion: that means that `levelChars` is an object accepting a key of type `string` ([x: string]), and the value can be of `any` type. This isn't a **real key**, but rather an intellisense suggestion, which **won't exist at runtime**. – briosheje Sep 21 '18 at 12:38
  • @briosheje is there any usefulness in that `[x: string]: any;` information? It's the same for all objects... – yaru Sep 21 '18 at 12:49
  • [**Do not post images of code or errors!**](https://meta.stackoverflow.com/q/303812/995714) Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly. – Rob Sep 21 '18 at 12:52
  • [What is an internal slot](https://stackoverflow.com/questions/33075262/what-is-an-internal-slot-of-an-object-in-javascript) this may help – ste2425 Sep 21 '18 at 12:58
  • 1
    @Rob There is no error message. OP is asking exactly about the tooltip that VSCode is showing, and posting a screenshot for that is fine. (Sure, posting the code to easily reproduce in your own VSCode installation would be a good addition). – Bergi Sep 21 '18 at 13:07
  • @ste2425 No, that has absolutely nothing to do with it – Bergi Sep 21 '18 at 13:08
  • @yaru If you had an object whose properties all had the same value type, I'd expect not to get `…: any`. – Bergi Sep 21 '18 at 13:09
  • @yaru : Yes, there is. You are aware that this object specifically can accept whatever kind of value and that the key must be a string. If, for some reason, all values were string, the intellisense would've suggested you [x: string]: string. Otherwise, you may encounter a situation where a well documented object will explicitely tell you that the signature is different. If you try to play a bit with typescript, you surely will get why the feature is there, and trust me: that's useful, as long as you remember that you need to forget about it at runtime. – briosheje Sep 21 '18 at 13:34
  • @Bergi Yes. There is no error message. There is no code either. – Rob Sep 21 '18 at 13:38

1 Answers1

1

[x: string]: any; is a type signature. It says that an object can have any number of properties, so it won't mark it as an error if you do i.e.

levelChars.sth = "sth";

which sometimes is exactly what you want but mostly is an error. Typescript would mark it as an error and give your object a strict signature (without [x: string]: any;).

marzelin
  • 10,790
  • 2
  • 30
  • 49