javascript - What is the difference between two code in typescript? -


i starting typescript , got stuck on issue.

i have 2 codes, in first code there object name , in code (same first code) variable name changed user. problem first code creating error first working fine.

first code (producing error)

interface person {     firstname : string;     lastname  : string; }  function greeter(person: person) {     return "hello " + person.firstname + " " + person.lastname; }  var name = {firstname: "girdhari", lastname: "agrawal"}; document.body.innerhtml = greeter(name); 

second code (working fine)

interface person {     firstname : string;     lastname  : string; }  function greeter(person: person) {     return "hello " + person.firstname + " " + person.lastname; }  var user = {firstname: "girdhari", lastname: "agrawal"};   document.body.innerhtml = greeter(user); 

please me understand this.

edited

this getting while compiling first script greeter.ts(10,5): error ts2403: subsequent variable declarations must have same type.  variable 'name' must of type 'string', here has type '{ firstname: string; lastname: string; }'. greeter.ts(13,35): error ts2345: argument of type 'string' not assignable parameter of type 'person'. 

it's because name global variable defined in lib.d.ts (window.name):

declare var name: string; 

the reason you're getting error because code in global scope. that's why using different variable name works.


Comments