Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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
Javascript 为什么在每个d.ts文件中都会出现typescript语法错误?_Javascript_Node.js_Typescript - Fatal编程技术网

Javascript 为什么在每个d.ts文件中都会出现typescript语法错误?

Javascript 为什么在每个d.ts文件中都会出现typescript语法错误?,javascript,node.js,typescript,Javascript,Node.js,Typescript,因此,在运行“node main.js”之后,每个d.ts文件中都会出现错误: declare function addTask(db: any): (params: any) => void; ^^^^^^^^ SyntaxError: Unexpected token 'function' at Object.compileFunction (vm.js:344:18) at wrapSafe (internal/modules/cjs/loader.

因此,在运行“node main.js”之后,每个d.ts文件中都会出现错误:

declare function addTask(db: any): (params: any) => void;
        ^^^^^^^^

SyntaxError: Unexpected token 'function'
    at Object.compileFunction (vm.js:344:18)
    at wrapSafe (internal/modules/cjs/loader.js:1106:15)
    at Module._compile (internal/modules/cjs/loader.js:1140:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at /home/vdm/dev/reminder/dist/actions/index.js:11:24
    at Array.forEach (<anonymous>)
addTask.d.ts:

declare function addTask(db: any): (params: any) => void;
export = addTask;

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "outDir": "dist",                        /* Redirect output structure to the directory. */
    "rootDir": "src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }
}

index.ts:

const fs = require('fs');
const path = require('path');

const loader = (db: any) => {
  const modules: {[index: string]:any} = {};
  const files = fs.readdirSync(__dirname);
  files.forEach((file: any) => {
    const modulePath = path.join(__dirname, file);
    const name = path.basename(file, '.js');
    if (name !== 'index') {
      const fn = require(modulePath)(db);
      modules[name] = fn;
    }
  });
  
  return modules;
};

export = loader;

index.js:

"use strict";
const fs = require('fs');
const path = require('path');
const loader = (db) => {
    const modules = {};
    const files = fs.readdirSync(__dirname);
    files.forEach((file) => {
        const modulePath = path.join(__dirname, file);
        const name = path.basename(file, '.js');
        if (name !== 'index') {
            const fn = require(modulePath)(db);
            modules[name] = fn;
        }
    });
    return modules;
};
module.exports = loader;

项目结构:

我不明白为什么在运行时会出现typescript错误?看起来节点正在尝试运行这些d.ts文件或idk。 如何解决这个问题?
ps:如果我在tsconfig中设置declaration:false,则错误消失,但我需要d.ts文件

您需要过滤掉
.ts
文件。NodeJS不知道如何解析TypeScript

const fs = require('fs');
const path = require('path');

const loader = (db: any) => {
  const modules: {[index: string]:any} = {};
  const files = fs.readdirSync(__dirname);
  files.forEach((file: any) => {
    if (file.endsWith('.js')) {
      const modulePath = path.join(__dirname, file);
      const name = path.basename(file, '.js');
      if (name !== 'index') {
        const fn = require(modulePath)(db);
        modules[name] = fn;
      }
    }
  });
  
  return modules;
};

export = loader;

这不是typescript错误,看起来您正在尝试运行typescript文件而不是编译的javascript。我正在dist folder中运行“node main.js”。您没有发布
index.ts
index.js
代码,但是当您需要
addTask
模块时,堆栈跟踪似乎表明发生了此错误。请参阅:
/home/vdm/dev/rementer/dist/actions/index.js:11:24
。添加了index.js和index.ts您必须筛选出扩展名为
.ts
的文件。使用
path.basename(文件'js')
对您来说并不适用。是的,我在查看标记为答案的loader=)时发现了这一点,因为有时当您试图向其他人解释问题时,它可能会帮助您了解问题的原因
const fs = require('fs');
const path = require('path');

const loader = (db: any) => {
  const modules: {[index: string]:any} = {};
  const files = fs.readdirSync(__dirname);
  files.forEach((file: any) => {
    if (file.endsWith('.js')) {
      const modulePath = path.join(__dirname, file);
      const name = path.basename(file, '.js');
      if (name !== 'index') {
        const fn = require(modulePath)(db);
        modules[name] = fn;
      }
    }
  });
  
  return modules;
};

export = loader;