Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Exporting/importing type and value with the same name

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda