使用高阶函数时,tsc会生成无用的d.ts文件

时间:2018-06-27 07:22:31

标签: typescript typescript-typings

我正在尝试为我的图书馆生成d.ts。这是我的tsconfig.json

{
  "compilerOptions": {
    "outDir": "dist",
    "declaration": true,
    "declarationDir": "dist/typings",
    "target": "es5",
    "diagnostics": true,
    "lib": [
      "es5",
      "es6",
      "dom"
    ],
    "noEmitOnError": true,
    "noImplicitAny": true,
    "noImplicitThis": false,
    "noUnusedParameters": false,
    "noUnusedLocals": false,
    "experimentalDecorators": true
  },
  "include": [
    "src/**/*",
    "typings/**/*.d.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

这是源代码:

// script.ts
import { Application } from "../application";
export class ScriptType { }
export function createScript(ScriptConstructor: typeof ScriptType) {
    return (app: Application) => {
        // do something;
        return ScriptConstructor;
    }
}

// orbitCamera.ts
import { createScript, ScriptType } from "../script";
class OrbitCamera extends ScriptType { }
export default createScript(OrbitCamera);

当我致电tsc生成d.ts时,得到以下结果:

$ cat dist/typings/scripts/camera/orbitCamera.d.ts
import { ScriptType } from "../script";
export declare class OrbitCamera extends ScriptType {
...
}
declare const _default: (app: import("../../../../../../../Users/u/Projects/p/src/application").Application) => typeof ScriptType;
export default _default;

这没有用,当我在其他项目中引用此d.ts文件时,它将抛出Cannot find name 'import'

那么我该如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

回滚到tsc 2.9.1,一切都会恢复。