What is the history of the creation and consumption of TypeScript libraries?

I use TypeScript here and there for web applications and refer to publicly defined type definitions available through Definitely Typed , but one thing that has always eluded me is how you could create reusable libraries in TypeScript for use by TypeScript application or another library.

Most of the recommendations on this topic seem to indicate how one could create or find type definitions for libraries originally created in JavaScript, but what about libraries written in TypeScript, there seems to be some kind of mechanism for sharing the resulting js files and the corresponding type definition file should be what is commonplace, but I could not find a single mention of who is trying to do this for private or public libraries. Perhaps I was looking for the wrong place? Is there a history of the creation and consumption of TypeScript libraries.

+8
libraries typescript
source share
2 answers

To create the TS library that will be used by the TS project, you do not need to do much.

(Sorry if the example is too detailed.)

Library

Assuming the source files are written in TypeScript, you need the following settings:

tsconfig.json

{ "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true, "target": "es5", "module": "commonjs", "removeComments": false, "sourceMap": true, "outDir": "dist/", "declaration": true }, "filesGlob": [ "**/*.ts", "!node_modules/**/*" ], "exclude": [ "node_modules", "typings/global", "typings/global.d.ts" ], "compileOnSave": true } 

The important thing here is basically declarations: true , which tells the TS compiler to generate d.ts files.

package.json

 { "name": "my-typescript-library", "description": "...", "version": "1.0.0", "main": "./dist/my.service.js", "typings": "./dist/my.service.d.ts", "license": "ISC", "dependencies": { ... }, "devDependencies": { "typescript": "^1.8.10", "typings":"^1.0.4", ... } } 

The important things here are the "core" and the "typing", which are the entry point for the service in the library. Therefore, if someone was require("my-typescript-library") , then the file specified here will be used. The type field is similar, but TypeScript obviously helps.

Then you transfer this library to Github, or Bitbucket, or elsewhere.

Consumer

You do not need much here.

package.json

Add a dependency on your library:

 { ..., "dependencies": { "my-typescript-library": "git+ssh://bitbucket.org/you/my-typescript-library", ... } } 

In this example, you will need the SSH key.

Then you just import the lib.

my_file.ts

 import {MyService} from "my-typescript-library"; 

So, you have this, TypeScript lib is used in a TypeScript application. Hope the answer is enough (and clear enough), otherwise just write me a line.

+4
source share

Well, I think you can use modules for this purpose, modules are like a namespace in C #, for example.

 //File Utils.ts module Utils { export class UtilsClass { do(param: string) { //do something } } } //Another ts file var formatter = new Utils.UtilsClass(); formatter.do("str"); 

Hi,

-one
source share

All Articles