从外部项目导入.d.ts类型

时间:2017-10-01 05:54:39

标签: typescript typescript2.0 .d.ts declaration-files

假设我的package.json中包含以下项目X:

  "typings": "lib/index.d.ts",
  "types": "lib/index.d.ts",

我想将index.d.ts文件中的所有类型导入另一个项目。

index.d.ts文件如下所示:

export declare const makePathExecutable: (runPath: string, cb: Function) => void;
export declare const checkForEquality: (arr1: string[], arr2: string[]) => boolean;
export declare const arrayHasDuplicates: (a: any[]) => boolean;
export declare const isStringWithPositiveLn: (s: string) => boolean;
export declare const findNearestRunAndTransform: (root: string, pth: string, cb: Function) => any;
export declare const findSumanMarkers: (types: string[], root: string, files: string[], cb: IMapCallback) => void;
export declare const isObject: (v: any) => boolean;

在我的其他项目中,我尝试导入这些类型:

import * as X from "x"

但这似乎并没有起作用。

导入这些类型的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您的项目x未与值分开声明类型。因此,要访问该类型,您需要使用typeof

import * as X from 'x'
type MakePathExecutable = typeof X.makePathExecutable

// or
import { makePathExecutable } from 'x'
type MakePathExecutable = typeof makePathExecutable