I\'ve been using TypeScript here and there for web applications and have referenced public type definitions made available through Definitely Typed but one thing that has always
well, I think that you can use modules for this purpose, modules is like namespace in c#, e.g.
//File Utils.ts
module Utils
{
export class UtilsClass
{
do(param: string)
{
//do something
}
}
}
//Another ts file
var formatter = new Utils.UtilsClass();
formatter.do("str");
Regards,
To create a TS library that will be consumed by a TS project you don't have to do a whole lot.
(Sorry if the example is overly verbose.)
Assuming your source files are writting in TypeScript, you need the following config tweaks:
{
"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 tell the TS compiler to generate the d.ts files.
{
"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 is "main" and "typings" which is the entry point for the service in the library. So if someone were to require("my-typescript-library")
then the file listed here would be used. The typings field is similar but obviously helps TypeScript.
You then push this library to Github, or Bitbucket, or where ever.
You don't need much here.
Add a dependency to your library:
{
...,
"dependencies": {
"my-typescript-library": "git+ssh://bitbucket.org/you/my-typescript-library",
...
}
}
You will need an SSH key in this example.
Then you just import the lib.
import {MyService} from "my-typescript-library";
So there you have it, a TypeScript lib being used in a TypeScript app. Hope that is enough of an answer (and clear enough), otherwise just drop me a line.