1

I'm trying some simple Javascript in the new Visual Studio Code. With the following js code:

var obj = { abc: "test" };
obj.foo = "bar";

I get this error:

Property 'foo' does not exist on type '{ abc: string; }'.

However it's a js file rather than ts. Isn't the code valid in Javascript?

Bruce55
  • 11
  • 2

2 Answers2

2

This is actually by design. It may be considered valid JavaScript, but it is not valid TypeScript Syntax.

See How do I dynamically assign properties to an object in TypeScript?, http://typescript.codeplex.com/workitem/1657, and http://typescript.codeplex.com/discussions/397908.

The good news is, JavaScript isn't compiled, so the file will still work correctly on your client, as tsc.exe will never really be run against the .js file.

Community
  • 1
  • 1
Claies
  • 22,124
  • 4
  • 53
  • 77
0

There is a rule to configure how these situation are handled. The default should be ignore in JavaScript but you can configure it to be a warning or an error with this:

{
    "javascript.validate.lint.unknownProperty": "ignore"
}
Johannes Rieken
  • 3,401
  • 1
  • 17
  • 10