Node.js 跨多个文件使用相同名称的未导出变量导致TS2403
我是一个尝试编写Node.js(Node.ts?)后端的TypeScript新手,并尝试进行设置服务器的基础知识。我正在使用ts节点版本8.6.2和typescript版本3.7.5,并以以下方式(以Node.js 跨多个文件使用相同名称的未导出变量导致TS2403,node.js,typescript,ts-node,Node.js,Typescript,Ts Node,我是一个尝试编写Node.js(Node.ts?)后端的TypeScript新手,并尝试进行设置服务器的基础知识。我正在使用ts节点版本8.6.2和typescript版本3.7.5,并以以下方式(以ajv样式)定义一些域对象的验证: 但是,ts node dev抛出以下错误: error TS2403: Subsequent variable declarations must have the same type. Variable 'commonSchema' must be of ty
ajv
样式)定义一些域对象的验证:
但是,ts node dev
抛出以下错误:
error TS2403: Subsequent variable declarations must have the same type.
Variable 'commonSchema' must be of type '{ type: string; properties: { foo: { type: string; }; }; }',
but here has type '{ type: string; properties: { bar: { type: string; }; }; }'.
我只能假设Typescript检测到了commonSchema
的这些单独声明的冲突,但它们没有被导出,所以我不确定这是如何发生的
关于TS及其在Node中的使用,我还有很多不了解的地方,但这是一个令人惊讶的行为,我想知道我是否有明显的遗漏。找到了一种方法让
TS Node dev
停止对我大喊大叫,这表明我的设置至少有一些问题。希望这个答案能为其他人提供足够的面包屑,帮助他们站在自己的积木上:
require('ts-node/register')
,因此我可以用现代ES语法编写代码。事实证明,这可能导致了双重编译问题,似乎有一些不可预测的下游影响ts node dev
的ts node
也需要通过设置ts\u node\u PROJECT
环境变量或传递--PROJECT(-p)标志来明确告知tsconfig.json
文件的位置// domain_obj_2.ts:
const commonSchema = {
type: 'object',
properties: {
bar: { type: 'string' },
},
};
export class DomainObject2 {
...
error TS2403: Subsequent variable declarations must have the same type.
Variable 'commonSchema' must be of type '{ type: string; properties: { foo: { type: string; }; }; }',
but here has type '{ type: string; properties: { bar: { type: string; }; }; }'.