导出/导入同名的类型和值

发布于 2025-01-09 01:46:58 字数 2066 浏览 0 评论 0原文

我正在尝试将 big.jstoFormat 在 typescript svelte 3 应用程序中。正如 GitHub 页面上所解释的,我已经安装了 DefinelyTyped 项目中的类型:

npm install big.js
npm install toformat
npm install --save-dev @types/big.js

但是,toFormat 没有类型定义;所以我写了自己的:

// toformat.d.ts
declare module 'toformat' {
    import type { BigConstructor, Big, BigSource } from 'big.js';

    export interface Decimal extends Big {
        toFormat(dp: number, rm?: number, fmt?: Object): string;
    }

    export interface DecimalConstructor extends BigConstructor {
        new (value: BigSource): Decimal;
    }

    export default function toFormat(ctor: BigConstructor): DecimalConstructor;
}

现在我可以像下面一样使用 big.js 和 toFormat (有效):

import toFormat from 'toformat';
import Big from 'big.js';
const Decimal = toFormat(Big);
console.log(new Decimal(12500.235).toFormat(2));

但是,我不想每次使用时都执行 toFormat,而是希望使用以下更简单的语法:

import Decimal from '../utilities/decimal'; // both type and value are imported here
let amount: Decimal;
amount = new Decimal(23.152);
console.log(amount.toFormat(2));

为此我创建了 / utility/decimal.ts 文件:

import Big from 'big.js';
import toFormat from 'toformat';
export type { Decimal } from 'toformat';
export default toFormat(Big);

现在的问题是 import Decimal from '../utilities/decimal'; 确实导入了 DecimalConstructor,但没有导入 Decimal 接口。我看到 import Big from 'big.js'; 同时导入 Big 接口和 BigConstructor;因此似乎可以将我的 Decimal 接口和 DecimalConstructor 放在同一个 Decimal 名称下。有人可以帮我解决这个问题吗?

更新:顺便说一句,以下工作:

import type { Decimal } from '../utilities/decimal';
import DecimalConstructor from '../utilities/decimal';
let amount: Decimal;
amount = new DecimalConstructor(23.152);
console.log(amount.toFormat(2));

我想要实现的是以与默认导入相同的名称导入 Decimal 和 DecimalConstructor 。

I'm trying to use big.js with toFormat in typescript svelte 3 app. As explained on the GitHub page, I've installed types from DefinitelyTyped project:

npm install big.js
npm install toformat
npm install --save-dev @types/big.js

But, there was no type definitions for toFormat; so I wrote my own:

// toformat.d.ts
declare module 'toformat' {
    import type { BigConstructor, Big, BigSource } from 'big.js';

    export interface Decimal extends Big {
        toFormat(dp: number, rm?: number, fmt?: Object): string;
    }

    export interface DecimalConstructor extends BigConstructor {
        new (value: BigSource): Decimal;
    }

    export default function toFormat(ctor: BigConstructor): DecimalConstructor;
}

Now I can use big.js and toFormat like below (which works):

import toFormat from 'toformat';
import Big from 'big.js';
const Decimal = toFormat(Big);
console.log(new Decimal(12500.235).toFormat(2));

But, instead of executing toFormat each time when I use, I'd like to have the following simpler syntax:

import Decimal from '../utilities/decimal'; // both type and value are imported here
let amount: Decimal;
amount = new Decimal(23.152);
console.log(amount.toFormat(2));

For that I created /utilities/decimal.ts file:

import Big from 'big.js';
import toFormat from 'toformat';
export type { Decimal } from 'toformat';
export default toFormat(Big);

The issue now is that import Decimal from '../utilities/decimal'; does import DecimalConstructor, but not Decimal interface. I saw that import Big from 'big.js'; imports both Big interface and BigConstructor; so it seems possible to put both my Decimal interface and DecimalConstructor under the same Decimal name. Can someone help me with this?

UPDATE: BTW, the following works:

import type { Decimal } from '../utilities/decimal';
import DecimalConstructor from '../utilities/decimal';
let amount: Decimal;
amount = new DecimalConstructor(23.152);
console.log(amount.toFormat(2));

What I' like to achieve is to import both Decimal and DecimalConstructor under the same name as default import.

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

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

发布评论

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

评论(1

幸福还没到 2025-01-16 01:46:58

经过一番试验和错误后,我使它按以下方式工作:

// /utilities/decimal.ts
import Big from 'big.js';
import toFormat from 'toformat';
import type { Decimal as Dec } from 'toformat';

const Constructor = toFormat(Big);
export const Decimal = Constructor;

// 'export default from' is not possible.
// See https://github.com/microsoft/TypeScript/issues/35010
// and https://github.com/tc39/proposal-export-default-from
export interface Decimal extends Dec { }
export default Decimal;

理想情况下,我不想重新声明 Decimal 接口,但我找不到任何更好的方法。

After some trial and error, I made it working the following way:

// /utilities/decimal.ts
import Big from 'big.js';
import toFormat from 'toformat';
import type { Decimal as Dec } from 'toformat';

const Constructor = toFormat(Big);
export const Decimal = Constructor;

// 'export default from' is not possible.
// See https://github.com/microsoft/TypeScript/issues/35010
// and https://github.com/tc39/proposal-export-default-from
export interface Decimal extends Dec { }
export default Decimal;

Ideally I'd like not to re-declare Decimal interface, but I couldn't find any better way.

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