与类同名的 Typescript 导入接口(interface)

标签 typescript class import interface

以下作品:

interface Foo {
  num: number;
}

class Foo {
}

下面不行,报错

Import declaration conflicts with local declaration of 'Foo'

import { Foo } from "./someModule";
class Foo {
}

这是有意为之的行为吗?如果我能够在一个模块中创建具有相同名称的接口(interface)和类,为什么不能导入?

最佳答案

两种情况下的行为是不同的。在第一种情况下,您最终不会得到一个接口(interface)和一个类,您最终会得到一个将两者合并的类。描述了行为 here .但是,这种行为不能跨模块发生。

如果你想扩充现有模块,你想要的行为称为模块扩充,也描述了 here .

关于与类同名的 Typescript 导入接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54500539/

相关文章:

javascript - 如何将具有通用 Prop 的 React 类组件转换为 Typescript 中的函数组件?

c++ - 错误 "C++ requires a type specifier for all declarations whilst defining methods"

c++ - 模板化成员函数的语法

java - 同一类中的类似方法

json - 如何在 React 中导入 json 文件

typescript - 错误 TS6059 : File is not under 'rootDir' .。 'rootDir' 预计包含所有源文件

typescript - WebStorm IDE 对 Vue 3 Composition API 的支持真的很差还是我的配置搞砸了?

javascript - 如何为根组件 dom 元素添加样式( Angular 选择器组件)

python - 无法让 NetworkX 读取我的加权网络(+尚未找到从文件导入节点属性的方法)

python - openpyxl 库 - jdcal 错误