Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share Typescript Types between Projects?

I have two projects (client and server) that should share the same definitions. To achieve this i outsourced my definition (.d.ts) in an extra npm package that I linked back to both projects via npm link. The types in the new npm package are not recognized by the two main projects. I tried package.json (types) settings and some settings in tsconfig.json (types, typeRoot) in various combinations, but I didn't get the right one.

What I have to set up in the definition package and the main packages to get that working?

like image 250
fastr.de Avatar asked Jun 12 '18 15:06

fastr.de


People also ask

How do I share TypeScript types between client and server?

Stages: (1) Copy client and server code, install client dependencies and build client. (2) Start afresh, copy server code only, install server dependencies and build server.

How do you add a TypeScript to a project?

You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.8). An alternative is to use npx when you have to run tsc for one-off occasions.

Is TypeScript front end or backend?

TypeScript is neither a frontend or backend language, but rather a superset of the already established and well-known software language, JavaScript.


1 Answers

types and typeRoots didn't work for me and I try many different paths/combinations (especially in typeRoots).

Finally I added the line

"include": [
    ...,
    "node_modules/<my-npm-package>/*.ts"
],

in tsconfig.json and my outsourced typings are available in the projects.

like image 136
fastr.de Avatar answered Oct 12 '22 23:10

fastr.de