简体   繁体   English

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

[英]Import .d.ts types from external project

Say I have a project X with the following in its package.json: 假设我在package.json中有一个项目X,其中包含以下内容:

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

I want to import all of the types from the index.d.ts file into another project. 我想将所有类型从index.d.ts文件导入另一个项目。

the index.d.ts file looks like: 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;

In my other project, I attempt to import these types like so: 在我的另一个项目中,我尝试像这样导入这些类型:

import * as X from "x"

but this doesn't really seem to work. 但这似乎并不奏效。

What is the right way to import these types? 导入这些类型的正确方法是什么?

Your project x does not declare the types separately from the values. 您的项目x不会将值分别声明为类型。 So to access the type, you need to use typeof : 因此,要访问类型,您需要使用typeof

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM