首页 > 解决方案 > 得到“错误 TS2304:找不到名称”'` 与 ts-node 编译

问题描述

我有这个tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "ts",
    "esModuleInterop": true,
    "sourceMap": true,
    "typeRoots": [
      "node_modules/@types",
      "./src/types"
    ]
  },
  "exclude": ["node_modues"]
}

我有这个文件夹结构:

src/
  scripts/
    doX.ts
  types/
    number.d.ts

number.d.ts我有:

type int8 = number

doX.ts中,我有:

function add8(a: int8, b: int8) {
  return a + b
}

const x: int8 = add8(1, 2)

我在 VSCode 中没有收到任何错误,但是当我执行以下操作时出现错误:

$ npx ts-node src/scripts/doX.ts 
⨯ Unable to compile TypeScript:
src/scripts/doX.ts:... - error TS2304: Cannot find name 'int8'.

... function add(a: int8, b: int8) {
                    ~~~~     ~~~~

我错过了什么ts-node才能正确编译和运行它?

标签: javascriptnode.jstypescriptts-node

解决方案


推荐阅读