Typescript 接口合并与导入&;当地人不工作?
接口合并的示例表明,可以在单个文件中合并接口。 但当接口位于不同文件中时,我无法合并它们 请注意,我没有扩充任何模块,没有模块,只有接口 /类型/索引d.tsTypescript 接口合并与导入&;当地人不工作?,typescript,typescript-typings,Typescript,Typescript Typings,接口合并的示例表明,可以在单个文件中合并接口。 但当接口位于不同文件中时,我无法合并它们 请注意,我没有扩充任何模块,没有模块,只有接口 /类型/索引d.ts export interface A { baseline: string } /someFile.ts import {A} from './types/index'; // <-- Error "Import declaration conflicts with local declaration of 'A'" import
export interface A { baseline: string }
/someFile.ts
import {A} from './types/index'; // <-- Error "Import declaration conflicts with local declaration of 'A'"
import {FancyInterface} from './SomeClass'
interface A {
someAdditionalFlavor: FancyInterface
}
export default class ABC implements A {}
从“/types/index”导入{A};// 记录答案,以便将来其他人更容易看到
您可以在导入时提供别名,如下所示
import { A as ImportedA } from './types/index';
import { FancyInterface } from './SomeClass'
interface A {
someAdditionalFlavor: FancyInterface
}
// Then use distinct A or ImportedA
您可以为导入提供别名<代码>从“…”导入{A as ImportedA}当你说“我没有扩充任何模块,没有模块,只有接口”时,你是在做一个错误的陈述。模块中声明的接口的作用域是该模块,就像模块中的其他所有声明一样。不可能跨模块合并接口声明。增强是您唯一的选择。@AluanHaddad您好,我来自Node.js背景,没有使用过TypeScript。你是什么意思?模块是指文件吗?所以不同的文件被认为是不同的模块,这就是你所说的吗?@VRoxa是一个很好的解决方法!在typescript中,如果文件包含一个或多个导入或导出,则该文件被视为模块。