简体   繁体   English

如何从 index.d.ts 中 typescript 中的多个文件中导出同名的两个模块/命名空间?

[英]How to export two modules/namespaces with same name from multiple files in typescript in index.d.ts?

I'm building a new npm package, I have two different typescript files, they contain namespaces and modules with same name 'X' at the end of each file i declare:我正在构建一个新的 npm package,我有两个不同的 typescript 文件,它们在我声明的每个文件的末尾包含名称空间和具有相同名称“X”的模块:

export default X;

I want to import them both into index.d.ts file and export them so the outer files (the files that import this repository/package) can import and use X modules and namespaces我想将它们都导入index.d.ts文件并导出它们,以便外部文件(导入此存储库/包的文件)可以导入和使用X模块和名称空间

But when I import them both:但是当我同时导入它们时:

import X from "./file1"
import X from "./file2"

I get this error:我收到此错误:

Duplicate identifier 'X'

Is there a way to have the same namespace in two different typescript file and export them to outer packages?有没有办法在两个不同的 typescript 文件中拥有相同的命名空间并将它们导出到外包?

Yes, there is - using aliases.是的,有 - 使用别名。

file1.ts文件1.ts

class A{}
export default A;

file2.ts文件2.ts

class A{}
export default A;

index.ts索引.ts

import { default as firstOne } from './file1';
import { default as secondOne } from './file2';
console.log(firstOne, secondOne);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何拆分大型的typescript index.d.ts文件 - How to split large typescript index.d.ts file 从typescript模块自动生成index.d.ts,类型定义 - Auto generate index.d.ts, type definitions, from a typescript module 如何在index.d.ts typescript定义文件中引用Redux类型? - How to reference Redux types in index.d.ts typescript definition file? 在index.d.ts构建错误中找不到名称“Record” - Cannot find name 'Record' in index.d.ts build error 如何从 index.d.ts 文件手动导入一个类型,并告诉 VS Code 智能感知一个变量是那种类型? - How to manually import a type from an index.d.ts file, and tell VS Code intellisense that a variable is of that type? index.d.ts 文件未发布到 NPM - index.d.ts file not published to NPM 组件无法从 React Native 中的 index.d.ts 文件自动导入类型 - Component fails to auto import types from index.d.ts file in React Native 错误:http://localhost/node_modules/@types/node/index.d.ts:严格模式下接口是保留字 - Error: http://localhost/node_modules/@types/node/index.d.ts: interface is a reserved word in strict mode 是否为canonical-json提供index.d.ts文件? - Providing an index.d.ts file for canonical-json? 用于角度库的DefinitelyTyped vs index.d.ts - DefinitelyTyped vs index.d.ts for angular library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM