Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 跨多个文件使用相同名称的未导出变量导致TS2403_Node.js_Typescript_Ts Node - Fatal编程技术网

Node.js 跨多个文件使用相同名称的未导出变量导致TS2403

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

我是一个尝试编写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 type '{ type: string; properties: { foo: { type: string; }; }; }',
but here has type '{ type: string; properties: { bar: { type: string; }; }; }'.
我只能假设Typescript检测到了
commonSchema
的这些单独声明的冲突,但它们没有被导出,所以我不确定这是如何发生的


关于TS及其在Node中的使用,我还有很多不了解的地方,但这是一个令人惊讶的行为,我想知道我是否有明显的遗漏。

找到了一种方法让
TS Node dev
停止对我大喊大叫,这表明我的设置至少有一些问题。希望这个答案能为其他人提供足够的面包屑,帮助他们站在自己的积木上:

  • 我在每个knex迁移文件的顶部都有
    require('ts-node/register')
    ,因此我可以用现代ES语法编写代码。事实证明,这可能导致了双重编译问题,似乎有一些不可预测的下游影响

  • 同样,支持
    ts node dev
    ts node
    也需要通过设置
    ts\u node\u PROJECT
    环境变量或传递--PROJECT(-p)标志来明确告知
    tsconfig.json
    文件的位置

  • 寻址#1似乎足以解决我所看到的错误,但为了正确起见,我也在寻址丢失的配置文件,因此prod中的最终编译是可预测的

    // 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; }; }; }'.