错误 TS2307:找不到模块 'marked' 或其对应的类型声明

error TS2307: Cannot find module 'marked' or its corresponding type declarations

我正在 Cypress 中执行无头测试,不得不 运行

npm install --save-dev start-server-and-test 这样服务器就可以启动并等待 url 响应,然后再 运行 进行测试。自从我 运行 该命令以来,我的代码一直在抛出以下错误。也不知道是不是巧合

Error: src/app/article/markdown.pipe.ts:2:25 - error TS2307: Cannot find module 'marked' or its corresponding type declarations.

2 import * as marked from 'marked';

这是我的 markdown.pipe.ts 文件:

import { Pipe, PipeTransform } from '@angular/core';
import * as marked from 'marked';

@Pipe({name: 'markdown'})
export class MarkdownPipe implements PipeTransform {
  transform(content: string): string {
    return marked(content, { sanitize: true });
  }
}

我尝试删除 node_modulespackage-lock.json 然后 运行 npm install 但这并没有解决问题。 我在 Whosebug 上搜索了类似的帖子,一些建议是 运行

npm install -g markednpm install --save-dev @types/marked 解决了一些类似的问题,但似乎没有解决我的问题。

这是文件夹的 git 存储库。 https://github.com/Leealp/BugsFixed

我该如何解决这个问题?

首先,为 marked 包添加类型

npm install --save @types/marked

index.d.ts 文件中,您可以看到

的几个变体
export function marked(...

这是“命名”导出,而不是“默认”导出(没有默认导出)

因此在 markdown.pipe.ts 中将其导入为

import {marked} from 'marked'