将javascript文件映射到打字稿定义文件

时间:2019-05-05 06:49:23

标签: typescript

我正在尝试将类型测试添加到仅包含index.d.ts的javascript项目中,并使用打字稿编写类型测试。其余的“真实”项目是用vanilla js编写的。

我的测试看起来像这样:

import { expectType } from 'ts-expect';
import {create} from 'src/file';

describe('Any', () => {
  it('creates something', () => {
    const a = create();
    expectType<Something>(a);
  });
});

问题是,import {create} from 'src/file';将解析为实际的物理文件并从javascript文件中推断类型。

我想以某种方式告诉编译器将index.d.ts文件用于这些定义。

我想做这样的事情,这绝对是行不通的:

  "paths": {
    "src/*": ["index.d.ts"],
    "*": ["./node_modules/@types/*"]
  }

这里是整个tsconfig.json,但我不知道如何将声明文件映射到javascript文件:

{
  "compilerOptions": {
      "module": "commonjs",
      "moduleResolution": "node",
      "lib": [
          "es6"
      ],
      "noImplicitAny": true,
      "noImplicitThis": true,
      "strictNullChecks": true,
      "types": [
          "mocha"
      ],
      "noEmit": true,
      "forceConsistentCasingInFileNames": true,
      "allowJs": true,
      "baseUrl": ".",
      "paths": {
        "src/*": ["src/*"],
        "src/*": ["index.d.ts"], // somehow map the js types to the ts types
        "*": ["./node_modules/*"]
      }
  },
  "include": [
      "types/**/*.test.ts"
  ]
}

我要执行此操作的唯一方法是将导入更改为此:

import {create} from 'package-name';

然后npm link该项目,使其认为它是其自身的依赖项。

有更好的方法吗?

0 个答案:

没有答案