Cannot redeclare block-scoped variable 'name' In TypeScript

后端 未结 3 1017
天命终不由人
天命终不由人 2021-02-12 17:38

Hi I am learning typescript.

I have in my code.

var name:string=\"Hello world\";
console.log(name);

while compile time I am getting thi

相关标签:
3条回答
  • 2021-02-12 18:03

    The name property is defined on the window object:

    interface Window extends EventTarget, WindowTimers, WindowSessionStorage, WindowLocalStorage, WindowConsole, GlobalEventHandlers, IDBEnvironment, WindowBase64, GlobalFetch {
        ...
        name: string;
        ...
    }
    

    (https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts#L17226)

    You'll need to come up with a new name for your variable:

    var myname = "Hello world";
    console.log(myname);
    
    0 讨论(0)
  • 2021-02-12 18:18

    You can add export{} at the beginning of your file.

    0 讨论(0)
  • 2021-02-12 18:30

    Your variable name has already been declared somewhere in the same block of code. And it is not allowed.

    This is exactly the meaning of the error message.

    The cause being that, you tried to declare this particular variable on global scope, and here name is already defined for some technical reason, for more details see : https://github.com/Microsoft/TypeScript/issues/9850

    (Thanks @betadeveloper )

    0 讨论(0)
提交回复
热议问题