如何在Angular CLI项目中使用@types/fhir

3
当我将这个库安装到我的CLI项目中并尝试引用其中的类型时,我得到了以下错误信息:
 error TS2306: File 'C:/ng-ikr-lib-test/node_modules/@types/fhir/index.d.ts' is not a module.

这是我的tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

以及我的应用程序 tsconfig,它扩展了上述内容。

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": ["fhir"]
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

您如何在angular-cli应用程序中使用此库中定义的类型?

https://www.npmjs.com/package/@types/fhir


能否提供一下你给出负评的原因? - cobolstinks
我在想,你是否需要安装这个库(https://www.npmjs.com/package/fhir)?通常情况下,你需要同时安装库和类型。我从未使用过这个库,所以这只是一个猜测。文档似乎对此并没有详细说明。 - R. Richards
2个回答

3

1
谢谢!这对于我们在Angular中处理FHIR响应的人来说非常有帮助。非常感激! - Brandon Taylor
1
不用谢,很高兴能帮到你。我知道这个问题曾经让我抓狂了一段时间。 - cobolstinks

1
我发现最简单的方法是在文件顶部使用以下行引用类型:
///<referencepath="../../../node_modules/@types/fhir/index.d.ts"/>

例如,我的fhir.service.ts文件顶部的引用如下所示:
///<reference path="../../../../node_modules/@types/fhir/index.d.ts"/>
import {Injectable} from '@angular/core';
import {Observable, throwError} from 'rxjs';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';

import Patient = fhir.Patient;
import Observation = fhir.Observation;
import Bundle = fhir.Bundle;
import Medication = fhir.Medication;

在“使用依赖项”部分,您可以在https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html找到更多的背景信息。


谢谢,似乎问题在于库的作者采用了特定的打包方式。这个库被打包成全局库,因此需要使用///引用方式。我最终重新打包了这个库,使其不在全局范围内运行。谢谢! - cobolstinks
@cobolstinks,你重新打包的库在npm上可用吗? - Brandon Taylor
1
@Brandon,它在内部工件存储库中...我需要查看许可证并查看是否可以将重新打包的库发布到公共npm注册表。 - cobolstinks
1
@cobolstinks 如果可能的话,那将非常有帮助。 - Brandon Taylor
1
@Brandon,这是MIT的项目,所以我应该没问题,我会尽量在今晚发布出来。 - cobolstinks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接