ts-node中同名JSON文件遮蔽TS模块,但TypeScript编译无报错的问题咨询
ts-node中同名JSON文件遮蔽TS模块,但TypeScript编译无报错的问题咨询
大家好,我遇到了一个TypeScript搭配ts-node使用时的诡异问题,想跟各位请教下:
当开启了resolveJsonModule编译选项后,如果项目里存在一个和TypeScript文件同名的JSON文件,TypeScript会出现非常反常的行为。
举个例子,我有以下几个文件:
myModule.json
{ "foo": "bar" }
myModule.ts
export function hello() { return 'Hello, world!'; }
elsewhere.ts
import { hello } from './myModule'; // TypeScript的类型检查显示这里`hello`的类型是`string` const message = hello();
按照TypeScript的错误检查结果来看,这个函数应该能被正确导入,但实际运行时调用hello()却会抛出异常——因为导入的hello值是undefined:
console.log(hello); // 输出:undefined hello() // 报错:TypeError: (0 , myModule_1.hello) is not a function
看起来这个问题的根源可能是TypeScript(或者也许是ts-node)尝试...
备注:内容来源于stack exchange,提问作者Maddy Guthridge
相关产品推荐
相关产品推荐

