简体   繁体   中英

Import .d.ts types from external project

Say I have a project X with the following in its package.json:

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

the index.d.ts file looks like:

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. So to access the type, you need to use typeof :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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