ts-node игнорирует объявление пользовательского типа - PullRequest
0 голосов
/ 02 июня 2019

Проблема в том, как говорится в теме. Интересная вещь - у меня есть другой проект с очень похожим конфигом и типами, работающими там хорошо.

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 работает нормально

...