1

In the below code I am getting an error in statement (emp:IEmployee= { FirstName: "Saurabh", LastName: "Vats" };) when am trying to define a variable as interface type. Why is TypeScript is giving me error here?

class Greeter {
    element: HTMLElement;
    span: HTMLElement;
    timerToken: number;
    constructor(element: HTMLElement) {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();
    }

    start() {
        this.timerToken = setInterval(() => this.span.innerHTML = new Date().toUTCString(), 500);
    }

    stop() {
        clearTimeout(this.timerToken);
    }

    print(employee: IEmployee) {
        console.log(employee.FirstName + employee.LastName);

    }

}

interface IEmployee {
    FirstName: string;
    LastName: string;
}
window.onload = () => {
    var el = document.getElementById('content');
    var greeter = new Greeter(el);
    greeter.start();
    emp:IEmployee= { FirstName: "Saurabh", LastName: "Vats" };
    greeter.print(emp);

};
3
  • 1
    It does not just give you "an error". It gives you a specific error message. Commented Aug 5, 2016 at 5:34
  • it gives me a error that cannot find IEmployee Commented Aug 5, 2016 at 5:36
  • 1
    That should have been in the question. Also, do not rephrase the error message, just add it exactly the way you get it. Commented Aug 5, 2016 at 5:42

1 Answer 1

2

You're just missing a keyword.

This is incorrect:

emp:IEmployee= { FirstName: "Saurabh", LastName: "Vats" };

It should be:

let emp:IEmployee= { FirstName: "Saurabh", LastName: "Vats" };


But what if, for some reason, you wanted to make it a global variable?

In javascript, if you had written:

emp = { FirstName: "Saurabh", LastName: "Vats" };

You would actually be adding this to the window object.

You can still do this in typescript, you just have to be explicit about it:

interface Window {
    emp:IEmployee;
}

window.onload = () => {
     var el = document.getElementById('content');
     var greeter = new Greeter(el);
     greeter.start();
     window.emp = { FirstName: "Saurabh", LastName: "Vats" };
     greeter.print(emp);
};
Sign up to request clarification or add additional context in comments.

6 Comments

let is working fine but it creates a variable in local scope, if I am not giving the let keyword I was expecting it to create a global variable but instead I am getting error
@saurabhvats Why on earth would you want to create a global variable, and why on earth would you imagine in an ES6 world that you could do that that way?
am new to typescript so I thought that as javascript creates global variable when you dont specify the var keyword, I thought typescript would behave in the same manner. its fine if it throws the error for variable without 'let' keyword
@saurabhvats I extended my question to say how you would create it as a global variable. (Not that I'm saying it's a good idea).
just noticed while defining global variable also am unable to say "window.emp:IEmployee" rather I have to say window.emp ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.