首页 > 其他分享 >[Typescript] Resolving the Block-scoped Variable Error in TypeScript (moduleDetection)

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

时间:2024-01-23 21:46:14浏览次数:27  
标签:Matt TypeScript force global Resolving moduleDetection Typescript file

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"
}

 

标签:Matt,TypeScript,force,global,Resolving,moduleDetection,Typescript,file
From: https://www.cnblogs.com/Answer1215/p/17983462

相关文章

  • [Typescript] Show the error close to where it's causes
    Examplecode:constroutingConfig={routes:[{path:"home",component:"HomeComponent",},{path:"about",component:12,},{path:"contact",componen......
  • TypeScript 实用技巧(下)
    第六部分:杂项原文:exploringjs.com/tackling-ts/pt_miscellaneous.html译者:飞龙协议:CCBY-NC-SA4.0下一步:23 使用类型进行计算的概述二十三、类型计算概述原文:exploringjs.com/tackling-ts/ch_computing-with-types-overview.html译者:飞龙协议:CCBY-NC-SA4.023.......
  • TypeScript 实用技巧(上)
    第一部分:起步原文:exploringjs.com/tackling-ts/pt_preliminaries.html译者:飞龙协议:CCBY-NC-SA4.0接下来:1 关于本书一、关于这本书原文:exploringjs.com/tackling-ts/ch_about-book.html译者:飞龙协议:CCBY-NC-SA4.01.1 这本书的主页在哪里?1.2 这本书包括什......
  • TypeScript 实用技巧(中)
    十四、向类型添加特殊值原文:exploringjs.com/tackling-ts/ch_special-values.html译者:飞龙协议:CCBY-NC-SA4.014.1 在带内添加特殊值14.1.1 向类型添加null或undefined14.1.2 向类型添加符号14.2 在带外添加特殊值14.2.1 辨别式联合14.2.2 其......
  • TypeScript基础知识
    TypeScript基础知识1.变量声明TypeScript在JavaScript的基础上加入了静态类型检查功能,因此每一个变量都有固定的数据类型。1letmsg:string='helloworld' let声明变量的关键字,const则代表常量;msg是变量名,自定义的;string是变量的数据类型。2.更多数据类型声明letm......
  • tsx nodejs 增强支持执行typescript 以及esm
    tsx属于一个nodejs的增强包含的特性快速的按需ts以及esm编译支持commonjs以及esm包支持下一代ts扩展(.cts,.mts)隐藏体验特性警告tsrepl解析tsconfig.json适用的场景可以执行运行ts而不需要进行配置更好的与esm,cjs依赖交互说明tsx是一个不错的本地开发工具,可以方便不同模......
  • [Typescript 5] override keyword
    OverridesAcommonmistake,thathashistoricallybeendifficultforTypeScripttoassistwithistyposwhenoverridingaclassmethodclassCar{honk(){console.log("beep")}}classTruckextendsCar{hoonk(){//OOPS!con......
  • [Typescript] Static block for Class
    InrecentTypescript,itispossibletodefinestaticblockclassCar{staticnextSerialNumber=100static{//thisisthestaticfieldfetch('https://get-next-serial-number.com').then(res=>res.json()......
  • TypeScript 中的 Export 和 Import
    TypeScript中的Export和ImportAUG 30TH, 2016 7:33AM在TypeScript中,经常要使用export和import两个关键字,这两个关键字和es6中的语法是一致的,因为TypeScript=es6+type!注意:目前没有任何浏览器实现export和import,要在浏览器中执行,必须借助TypeSc......
  • Vue 3 + TypeScript + Vite + Element-Plus + Router + Axios + Pinia项目搭建(内含完
    Vue3+TypeScript+Vite+Element-Plus+Router+Axios+Pinia项目搭建(内含完整架构)安装Vue3+ts+vitenpminitvite@latest选择y,新建项目名称,选择vue,选择vue-ts下载完成后执行以下命令行cd新建的项目名称npminpmrundev安装Element-Plusnpminstallelement-plus-......