创建和使用TypeScript库的故事是什么? [英] What is the story for creating and consuming TypeScript libraries?

查看:65
本文介绍了创建和使用TypeScript库的故事是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Web应用程序中使用TypeScript,并引用了通过绝对类型提供的公共类型定义.一直让我感到困惑的是,如何在TypeScript中创建可重用的库,以被TypeScript应用程序或另一个库使用.

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 eluded me is how one would go about creating reusable libraries in TypeScript with the purpose of being consumed by a TypeScript application or another library.

关于该主题的大多数指南似乎都直接指向如何为最初用JavaScript编写的库创建或找到类型定义,但是用TypeScript编写的库又如何呢?似乎有些机制可以共享生成的js文件和相应的类型定义文件应该是很常见的东西,但是我找不到任何人试图对私有或公共图书馆进行此操作.也许我在找错地方了?是否有创建和使用TypeScript库的故事.

Most guidance on the topic seems to point directly to how one would create or find type definitions for libraries authored originally in JavaScript but what about libraries written in TypeScript, it seems that some mechanism for sharing the resulting js files and a corresponding type definition file should be something that is common place but I haven't been able to find any mention of anybody trying to do this for private or public libraries. Perhaps I'm looking in the wrong places? Is there a story for creating and consuming TypeScript libraries.

推荐答案

要创建将由TS项目使用的TS库,您无需做很多事情.

To create a TS library that will be consumed by a TS project you don't have to do a whole lot.

(很抱歉,示例过于冗长.)

假设您的源文件是用TypeScript编写的,则需要进行以下配置调整:

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
}

这里重要的基本上是declarations: true,它告诉TS编译器生成d.ts文件.

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",
    ...
  }
}

这里重要的是"main"和"typings",这是库中服务的入口点.因此,如果有人要require("my-typescript-library"),则将使用此处列出的文件.键入字段类似,但显然有助于TypeScript.

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.

然后您将此库推送到Github,Bitbucket或任何地方.

You then push this library to Github, or Bitbucket, or where ever.

这里不需要很多.

向您的库添加依赖项:

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

在此示例中,您将需要SSH密钥.

You will need an SSH key in this example.

然后,您只需导入库.

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

因此,有了TypeScript应用程序中使用的TypeScript库,您就可以使用它.希望这是一个足够的答案(并且足够清晰),否则请给我留言.

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.

这篇关于创建和使用TypeScript库的故事是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆