导出/导入同名的类型和值
我正在尝试将 big.js 与 toFormat 在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番试验和错误后,我使它按以下方式工作:
理想情况下,我不想重新声明 Decimal 接口,但我找不到任何更好的方法。
After some trial and error, I made it working the following way:
Ideally I'd like not to re-declare Decimal interface, but I couldn't find any better way.