steveg
steveg

Reputation: 93

How do I share many Typescript interfaces across projects

I've made an project that only holds typescript interfaces. So I might have files like account.ts and item.ts that only contain interfaces

export interface Account
{
    name: string;
}

How do I package up all those files locally so I can npm install (from local) and use those interfaces in other projects with something like...

import { Account, Item } from 'all-my-interfaces';

Upvotes: 0

Views: 1031

Answers (1)

Nilay Vishwakarma
Nilay Vishwakarma

Reputation: 3353

I wrote an article on medium which addresses this.


A quick walkthrough with only essentials:

> mkdir my-lib
> cd my-lib
> npm init -y
> npm i -D typescript
> touch tsconfig.json
> mkdir src
> touch src/index.ts
> touch .npmignore

tsconfig.json:

{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "declaration": true,
      "outDir": "./dist",
      "strict": true
    }
}

src/index.ts:

export interface Check {
    isValid(): bool;
}

.npmignore:

dist/

Add the npm scripts in package.json:

"prepublishOnly": "tsc"

Publish:

npm publish

Now use in some other project:

npm i my-lib

Upvotes: 1

Related Questions