【问题标题】:Import .d.ts types from external project从外部项目导入 .d.ts 类型
【发布时间】:2018-03-12 14:06:58
【问题描述】:

假设我有一个项目 X,它的 package.json 中有以下内容:

  "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"

但这似乎并没有真正起作用。

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

【问题讨论】:

  • import * as X from "x" 好像导入了.js文件,但我只想导入类型

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


【解决方案1】:

您的项目x 没有将类型与值分开声明。所以要访问类型,需要使用typeof

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2016-11-09
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多