Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在我的角度库的node_modules文件夹之外使用*.d.ts?_Javascript_Angular_Typescript_Package_Angular8 - Fatal编程技术网

Javascript 如何在我的角度库的node_modules文件夹之外使用*.d.ts?

Javascript 如何在我的角度库的node_modules文件夹之外使用*.d.ts?,javascript,angular,typescript,package,angular8,Javascript,Angular,Typescript,Package,Angular8,我正在创建一个angular 8库,它将利用insane.js npm包。因为Inase是JavaScript,所以我需要创建它,以便我的typescript服务能够识别Inase函数。我使用dts gen创建了一个insane.d.ts文件,因为没有@types/insane包。但是,我不能在我的服务中使用导入,除非我将insane.d.ts文件放在/node\u modules/insane/文件夹中。也就是说,我不知道如何把这个文件和我的代码放在一起,如何识别这个疯狂的函数。每次我将文件移

我正在创建一个angular 8库,它将利用insane.js npm包。因为Inase是JavaScript,所以我需要创建它,以便我的typescript服务能够识别Inase函数。我使用dts gen创建了一个insane.d.ts文件,因为没有@types/insane包。但是,我不能在我的服务中使用导入,除非我将insane.d.ts文件放在/node\u modules/insane/文件夹中。也就是说,我不知道如何把这个文件和我的代码放在一起,如何识别这个疯狂的函数。每次我将文件移出该目录时,我在导入行上都会收到一个错误,上面写着:

找不到模块“疯狂”的声明文件c:/TFS/repo/amrock simple mde/projects/simple mde/node_modules/insane/insane.js'隐式具有“any”类型

我提供了一个stackblitz链接来帮助描述我的经历。我希望有帮助。查看共享文件夹下的SanitizerService,了解我正在尝试做什么。我正在尝试做一些事情,效果如下:

import { Injectable } from '@angular/core';
import * as insane from 'insane';  // line where error is

@Injectable({
  providedIn: 'root'
})

export class SanitizerService {

  constructor() { }

  sanitize(content: string): string {
    return insane(content, {
     allowedAttributes: {
       a: ['name', 'target']
     }
   });
  }

}
我希望进口能识别疯狂的d.t,但它没有。有人能帮我找出我遗漏了什么吗?

您需要使用一个:

声明模块“疯狂”{
// ...
}
基本上,将整个
.d.ts
文件包装在
声明模块
块中,并删除所有现有的
声明
关键字,因为这些关键字在
声明模块
块中无效

其次,确保TypeScript包含
.d.ts
文件。通常最简单的方法是在
tsconfig.json
include
选项中指定它


您的解决方案非常接近,但它得到了以下错误:错误类型错误:insane不是AppComponent.ngonit(app.component.ts:23)处的SanitizerService.sanitize(sanitizer.service.ts:25)处的函数,而是checkAndUpdateDirectiveLine(provider.ts:212)处的函数,checkAndUpdateNodeLine(view.ts:429)debugCheckDirectivesFn(services.ts:392)的checkAndUpdateNode(view.ts:389)和debugCheckDirectivesFn(services.ts:392)的checkAndUpdateNode(view.ts:389)和Object.eval[as updateDirectives](AppComponent_Host.ngfactory.js?[sm]:1)是一个运行时错误,这意味着它与类型声明没有直接关系。我猜模块希望以不同的方式导入。通常,使用调试器或日志找出
inase
的值会有所帮助。您还可以尝试导入默认导出:
import insane from'insane'