Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Import default from CommonJS module, export additional type from typing file

Legacy lib.js:

function Foo () {...}
Foo.a = function() {...}
module.exports = Foo

Typing lib.d.ts:

declare module "foo" {
  type Type = "a"|"b"|"c"
  interface Foo {
    (a: Type): string
    ...
  }
  export = Foo
  // how do i export Type??
}

Consumer app.ts:

import Foo = require('foo')
// how do i get Type from lib.d.ts??
like image 697
bcherny Avatar asked Dec 18 '25 12:12

bcherny


1 Answers

This is a really old question, but I needed to answer it myself. If in this case "Foo" is the default modules.exports, then you can use export default on Foo in the module declaration:

declare module "foo" {
  export type Type = "a"|"b"|"c" // export any custom types you like

  export default interface Foo { // default works
    (a: Type): string
    ...
  }
}

Then elsewhere you can do:

import Foo, { Type } from 'foo'
like image 60
brainbag Avatar answered Dec 21 '25 01:12

brainbag