Export and publish all types and interfaces in Typescript

Viewed 5592

I am trying to publish a typescript library. It is not clear to me how to expose all the types and interfaces. Here is my setup:

mylib
  ├──src
  │  ├──types
  |  |  └──mytypes.ts
  │  └──index.ts
  ├──package.json
  └──tsconfig.json
//index.ts

import {MyInterface} from './types/mytypes.ts';

const my_main_name = {
  myfunc: function(param1:MyInterface):void{
    console.log(param1.a);
  }
}

//mytypes.ts

export interface MyInterface {
  a:string
}

I am generating declaration files by setting tsconfig.json param "declaration":"true".

Now I want to use this libraries as dependency of another one.

mylib
yrlib
  ├──src
  │  └──index.ts
  ├──package.json
  └──tsconfig.json
//yrlib/package.json
{
 ...
 "dependencies":{
    "mylib":"file:../mylib/"
  }
}

//yrlib/src/index.ts

import * as mylib from 'mylib';

let a:mylib.MyInterface = ...
      // ~~ Namespace has no export member for MyInterface

I get autocomplete for mylib because it is exported.

My question is how can I see all the types and interfaces of mylib module inside yrlib?

Since they can be hundreds and in all different files and folders.

Should I export all of them one by one? What is the best practice? Is there a way to export all types at once?

And how "typying":"dist/index.d.ts" in package.json fits in all this? Should be all the types in one file? Why Typescript doesn't generate this file?

Should I use a namespace that contain everything? I cannot find a full explanation on this topic.

1 Answers

I recently faced the same problem and the only way I found in order to publish your Type-Space code (e.g.: types, interfaces) together with your Value-Space code (e.g.: function, classes, variables) is to export a namespace that contains everything.

Another odd thing is that in order to export into a namespace something imported, the syntax is:

import * as mymod from './mymod';
import * as mytypes from './mytypes';

export namespace mylib {
  export import mod = mymod;
  export import types = mytypes;
}

With the export import syntax.

Unfortunately this in my eslinter will resolve in a warning/error since apparently it reads mod and types as unused-vars.

I don't understand why there is no a full explanation for this topic in the documentation.

Related