Typescript - Can I "bolt-on" types from one module to another module?

Viewed 622

Background

Node has a package called path which has a type definitions file associated with it in the @types/node npm package. Someone made a client-side package called path-browserify which has the same api but has no types.

Question

Can I bolt on the @types/node types for path to use with path-browserify? Basically I want to use types in one place with a different name. I've tried this:

// types.d.ts
declare module 'path-browserify' {
  export {default} from 'path'
}

// usage.ts
import * as path from 'path-browserify'

const joined = path.join('/', 'file');
// ^ COMPILE ERROR: join not found

const joined = path.default('/', 'file'); 
// ^ RUNTIME ERROR: default not found 

But I'm unable to access the api correctly. Is there a better way to use these type definitions? preferably without copying the path api.

1 Answers

You look like you're on the right track. Can you do something like this?

declare module 'path-browserify' {
  import path from 'path'
  export default path
}

As for your usage, try the following (rather than using * as syntax)

import path from 'path-browserify'
path.resolve(...)

As an aside, does the path module even have a default export ... ?

Related