ts-node忽略自定义类型声明

时间:2019-06-02 19:53:14

标签: node.js typescript ecmascript-6 ts-node

问题正如主题所述。有趣的是-我还有另一个项目,其配置和类型非常相似。

package.json

{
  "name": "untitled",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "ts-node test.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^12.0.4",
    "fs": "0.0.1-security",
    "jszip": "^3.2.1",
    "ts-node": "^8.2.0",
    "typescript": "^3.5.1"
  }
}

tsconfig.json

{
   "compilerOptions": {
      "lib": [
         "es6",
         "dom"
      ],
      "typeRoots" : [
         "./node_modules/@types",
         "."
      ],
      "target": "es6",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": false
   },
   "exclude": [
      "node_modules",
      "**/*.spec.ts",
      "**/*.test.ts"]
}

index.d.ts

declare interface Array<T> {
    random() :T;
}

declare namespace NodeJS {
    export interface Global {
        __stack: any;
        __line: any;
        __function: any;
        sleep: Function;
    }
}

这是一个用于测试目的的简单项目-index.d.ts也位于项目的根目录中(也试图移动index.d.ts-> typings/index.d.ts)(当然,新的tsconfig.json也是如此)路径)。

每次尝试都以

结尾
TSError: ⨯ Unable to compile TypeScript:
polyfills.ts:1:17 - error TS2339: Property 'random' does not exist on type 'any[]'.

polyfills.ts

Array.prototype.random = function () {
    return this[Math.floor((Math.random()*this.length))];
};

这些错误。 请帮忙:)

更新

tsc && node build/test.js工作正常

0 个答案:

没有答案