tommy
tommy

Reputation: 733

Simple issue with Typescript and VSCode

I am trying to set up a VSCode environment for working with TypeScript v2.03

I start out with a simple vanilla javascript snippet I can test out in node via the integrated terminal.

function Person() {
    this.name = "";
}

Person.prototype.age = 0;

let me = new Person();

I insert into the Typescript Playground website and it compiles fine with no complaints.

But when I create a new file in VSCode, the VSCode editor complains:

enter image description here

And if I run webpack with the ts-loader plugin I get this error: enter image description here

When I run 'tsc src/test.ts' it compiles without complaints.

My questions:

  1. Is my code wrong or is this just a complaint I'm supposed to ignore?
  2. Is there anyway to tell VSCode to stop showing me the complaint?

Upvotes: 2

Views: 1268

Answers (1)

Igor
Igor

Reputation: 62298

One of the main benefits to TypeScript over JavaScript is explicitly defined types. In this case a better way to configure Person would be to define it as a class. This will restrict assignment to the known members on the type Person.

class Person {
    name: string;
    age: number;

    constructor(){
      this.name = "";
    }
}

let me = new Person();
// assignments to known members on the type Person
me.age = 21;

Upvotes: 2

Related Questions