Import .d.ts types from external project

791 views Asked by At

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?

1

There are 1 answers

0
unional On

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