用默认值和命名导出构建打字稿库?

发布于 2025-01-24 15:53:11 字数 456 浏览 7 评论 0原文

我喜欢如何将React导入为默认对象,例如从“ React”导入React并以破坏性的方式导入,例如import {useffect}从'react' 导入 *作为“ React”的任何内容如果您感到厚脸皮。我正在尝试为NPM构建自己的打字稿库,这些库可以以这三种方式导入(除了通常的需要语法,我已经花了大约15个小时了无法让它起作用(令人沮丧)

。并将它们放入 /lib /类型。

但是我真正坚持的是如何导出我的文件。类似于:

farvor * from'./filename1' 从'./filename2'导出 * ...

但这显然不能为我的库提供默认导出。我需要做一些索引模块声明魔术吗?建立打字稿库的任何提示 /教程都将有所帮助。我已经能够获得默认导入和命名为我的库的导入,但并非同时使用。

I like how React can be imported as a default object like import React from 'react' and imported in a destructured way, like import { useEffect } from 'react' or even import * as Whatever from 'react' if you're feeling cheeky. I'm trying to build my own typescript library for npm that can be imported in all 3 of these ways (in addition to the usual require syntax, and I've spent about 15 hours on it so far without being able to get it to work (frustrating).

Right now I'm using rollup to build lib/cjs and lib/esm, to support both CommonJS and Modules. I'm also having typescript-compiler (tsc) build type files and put them inside /lib/types.

But what I'm REALLY stuck on is how to export my files like how React does it. So far I'm using an index.ts "barrel file" at the root of my library, which is something like:

export * from './filename1'
export * from './filename2'
...

But this obviously doesn't provide a default export for my library. Is there some sort of index.d.ts module declaration magic I need to do? Any tips / tutorials on building a typescript library would be helpful. I've been able to get both default imports and named imports to work for my library, but not both at the same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2025-01-31 15:53:11

您可以先导入所有内容,然后使用导出默认导出所有内容。

import * as filename1 from './filename1';
import * as filename2 from './filename2';

export default {
  ...filename1,
  ...filename2
};

这个适合您吗?

You can import everything first and then use export default to export everything.

import * as filename1 from './filename1';
import * as filename2 from './filename2';

export default {
  ...filename1,
  ...filename2
};

Does this work for you?

浅唱ヾ落雨殇 2025-01-31 15:53:11

这应该是评论,但请参阅此答案。您可以将导出和单个文件中的单数默认导出组合在一起。您必须做一些工作以按照您想要的方式构建默认对象,但是以下内容应起作用:

import * as filename1 from './filename1';
import * as filename2 from './filename2';

// build defaultObject as desired
const defaultObject = Object.assign({}, filename1, filename2)

export default defaultObject;
export * from './filename1';
export * from './filename2';

This should be a comment, but refer to this answer. You can combine exports and a singular default export in a single file. You'll have to do some work to build the default object the way you want it but the following should work:

import * as filename1 from './filename1';
import * as filename2 from './filename2';

// build defaultObject as desired
const defaultObject = Object.assign({}, filename1, filename2)

export default defaultObject;
export * from './filename1';
export * from './filename2';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文