[Typescript] Resolving the Block-scoped Variable Error in TypeScript (moduleDetection)

const NAME = "Matt";

TypeScript is telling us we can't redeclare the name variable because it has already been declared inside of lib.dom.d.ts.

The index.ts file is being detected as a global script rather than a module. This is because, by default, TypeScript assumes that a file without any imports and exports is meant to be a global script.

 

If we add a export then it resolve problem:

const name = "Matt"

export {}

 

If we want to do it globally, we can use 'moduleDetection' option in tsconfig to force all js file are module

{
	"moduleDetection": "force"
}

 

posted @ 2024-01-23 21:32  Zhentiw  阅读(13)  评论(0编辑  收藏  举报