在库中包含自定义打字稿声明文件

时间:2018-10-18 22:01:00

标签: typescript typescript-typings type-declaration .d.ts typescript-declarations

我在弄清楚如何在要创建的库中包含自定义打字稿声明文件时遇到了一些麻烦。让我给你一点背景。

我正在编写一个库,用于处理医疗保健平台规范FHIR(http://hl7.org/fhir/)中的资源。

为此,我创建了一个d.ts文件,指定了我在库中使用的所有资源以确保类型安全。

在我的ts文件中,我引用了d.ts文件,在其中我声明了fhir资源,但是一旦将该库与webpack捆绑在一起并导入到另一个项目中,就找不到包含我的fhir资源的声明文件

以下一些代码段可能会有所帮助:

fhir.d.ts

/** Module containing interfaces for all used FHIR resources in accordance with FHIR version 3.5.0 */
declare module 'fhir' {
  /**
   *    Any combination of upper or lower case ASCII letters ('A'..'Z', and 'a'..'z', numerals ('0'..'9'), '-' and '.', with a length limit of 64 characters. (This might be an integer, an un-prefixed OID, UUID or any other identifier pattern that meets these constraints.)
   */
  type id = string
  /**
   * A Uniform Resource Identifier Reference (RFC 3986 ). Note: URIs are case sensitive.
   */
  type uri = string
  /**
   * A URI that refers to a canonical URI.
   */
  type canonical = uri
  /**
   * A stream of bytes, base64 encoded (RFC 4648).
   */
  type base64Binary = string
  /**
   * A string which has at least one character and no leading or trailing whitespace and where there is no whitespace other than single spaces in the contents
   */
  type code = string
  /**
   * A date, or partial date (e.g. just year or year + month) as used in human communication. The format is YYYY, YYYY-MM, or YYYY-MM-DD, e.g. 2018, 1973-06, or 1905-08-23. There SHALL be no time zone.
   */
...
[shortened for brevity]

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "typings",
    "removeComments": false,
    "module": "commonjs",
    "noImplicitAny": true,
    "outDir": "dist",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "types": ["jasmine"],
    "resolveJsonModule": true,
    "esModuleInterop": true
  },
  "exclude": ["node_modules"]
}

package.json

{
  "name": "<project-name>",
  "version": "1.1.1",
  "description": "<project-description>",
  "main": "./dist/main.js",
  "typings": "./typings/src/main.d.ts",
  "scripts": {
    "build": "npm run clean && webpack --mode production",
    "clean": "rimraf dist build coverage",
    "lint": "tslint --project tsconfig.json 'src/**/*.{ts,tsx}'",
    "test": "karma start"
  },
  "devDependencies": {
    "@types/es6-promise": "^3.3.0",
    "@types/jasmine": "^2.5.35",
    "jasmine-core": "^2.4.1",
    "karma": "^2.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-typescript": "^3.0.13",
    "prettier": "^1.14.3",
    "ts-loader": "^5.1.0",
    "tslint": "^5.11.0",
    "tslint-config-prettier": "^1.15.0",
    "tslint-config-standard": "^8.0.1",
    "typescript": "^3.0.3",
    "webpack": "^4.19.0",
    "webpack-cli": "^3.1.0"
  }
}

你能帮我吗?

0 个答案:

没有答案