无法编译简单的 TypeScript node-ts 示例

Unable to compile simple TypeScript node-ts example

我正在努力学习https://github.com/TypeStrong/ts-node。我写了两个文件作为一个简单的例子。它们都在同一个目录中,目前与 package.json

不在同一个文件夹中
/package.json
/src/build-lib/run-ts.js
/src/build-lib/Test.ts

运行-ts.js

require('ts-node').register();
const Test = require('./Test.ts').Test; // Tried with and without .ts extension
const tester = new Test();
tester.log('Message');

Test.ts

export class Test {
    log(message: string) {
        console.log(`Test ${message}`);
    } 
}

我运行正在编写以下脚本:

ts-node src/build-scripts/gen-xml.js

我收到以下编译错误

/Users/jmendes/gitclient/vcd-ui/content/core/node_modules/ts-node/src/index.ts:319
      throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
            ^
TSError: ⨯ Unable to compile TypeScript
src/build-scripts/Test.ts (7,36): Parameter 'message' implicitly has an 'any' type. (7006)
    at getOutput (/Users/jmendes/gitclient/vcd-ui/content/core/node_modules/ts-node/src/index.ts:319:17)
    at /Users/jmendes/gitclient/vcd-ui/content/core/node_modules/ts-node/src/index.ts:350:18

我希望在控制台上看到 "Test Message"。

您似乎有一个 JavaScript 文件 (运行-ts.js),它本身正在注册 TypeScript 以拦截任何 require 次通话。

您是否尝试过 运行仅使用 node 而不是 ts-node 来创建该文件?

我的解决方法是从 package.json 中删除 ts-nodetypescript,然后:

npm install ts-node --save-dev
npm install -g typescript --save-dev