当我npm将这个库安装到我的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应用程序中使用此库中定义的类型?
答案 0 :(得分:2)
答案 1 :(得分: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下的“消费依赖项”部分中找到更多背景信息。