typescript - 如何修复 typescript 中的 'Cannot use namespace as a type ts(2709)'?

标签 typescript

我正在加载一个名为 sorted-array 的第三方库并像这样使用它:

import SortedArray from 'sorted-array';

export class Selector {
  private mySortedArray!: SortedArray;

  constructor() {
    this.mySortedArray = new SortedArray();
  }
}

但是,我收到此错误:Cannot use namespace 'SortedArray' as a type.ts(2709)
所以,我创建了这个文件:
// src/typings/sorted-array/index.d.ts
declare module 'sorted-array' {
  class SortedArray {
    constructor(arr: number[]);
    search(element: any): number;
  }
}

但是,错误仍然存​​在。我究竟做错了什么?

最佳答案

我一直在努力弄清楚如何编写类型定义来传递外部/第 3 方模块。我对 TypeScript 非常不聪明也不敏锐,但是 TypeScript 2.9's import() syntax似乎是我一直在寻找的答案(经过长时间的笨拙,被误导):

declare type NewRelicModule = typeof import("newrelic");
现在我可以写我的:
interface Config {
  newrelic?: NewRelicModule;
}
看起来您希望使用默认导出。也许对你来说这可能有用?
declare type SortedArray = typeof import("sorted-array").default;

关于typescript - 如何修复 typescript 中的 'Cannot use namespace as a type ts(2709)'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853815/

相关文章:

javascript - 如何使用 Moment.js 更改日期?

node.js - 包含箭头函数捕获全局值 'this'

typescript - 保存时编译 Visual Studio Code

angular - 如何在我的 Angular 12 应用程序中实现 firebase app-check

javascript - TS2339 : Property 'style' does not exist on type 'Element'

html - 从 formbuilder 中获取一个 id 以删除 Angular 6 中的一行

javascript - 保留字符串格式中提到的空格

angular - 类型“订阅”上不存在属性 'subscribe'

typescript - TypeScript 类型别名是否应该始终大写?

javascript - 如何传递变量以在 Angular 2 的 Accordion 中切换特定面板