Skip to content

Commit

Permalink
TS declaration declares currency global (#297)
Browse files Browse the repository at this point in the history
* TS declaration declares currency global

ISSUE: The currency export is also declared globally as a namespace const and constructor.

When importing currency.js once in a module, TypeScript loads this declaration file and declares `currency` global and assumes it's available in all modules. This is not true and will give runtime errors. Exporting`currency` inside the module declaration ensures it's is imported in every module that is using it because TS will give compile errors.

* formatting

Co-authored-by: Jason <jason@scurker.com>
  • Loading branch information
PVermeer and scurker committed Mar 9, 2021
1 parent e5d682b commit 4a7946a
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/currency.d.ts
@@ -1,42 +1,42 @@
declare namespace currency {
type Any = number | string | currency;
type Format = (currency?: currency, opts?: Options) => string;
interface Constructor {
(value: currency.Any, opts?: currency.Options): currency,
new (value: currency.Any, opts?: currency.Options): currency
}
interface Options {
symbol?: string,
separator?: string,
decimal?: string,
errorOnInvalid?: boolean,
precision?: number,
increment?: number,
useVedic?: boolean,
pattern?: string,
negativePattern?: string,
format?: currency.Format,
fromCents?: boolean
declare module 'currency.js' {
namespace currency {
type Any = number | string | currency;
type Format = (currency?: currency, opts?: Options) => string;
interface Constructor {
(value: currency.Any, opts?: currency.Options): currency,
new(value: currency.Any, opts?: currency.Options): currency
}
interface Options {
symbol?: string,
separator?: string,
decimal?: string,
errorOnInvalid?: boolean,
precision?: number,
increment?: number,
useVedic?: boolean,
pattern?: string,
negativePattern?: string,
format?: currency.Format,
fromCents?: boolean
}
}
}

declare interface currency {
add(number: currency.Any): currency;
subtract(number: currency.Any): currency;
multiply(number: currency.Any): currency;
divide(number: currency.Any): currency;
distribute(count: number): Array<currency>;
dollars(): number;
cents(): number;
format(opts?: currency.Options | currency.Format): string;
toString(): string;
toJSON(): number;
readonly intValue: number;
readonly value: number;
}
interface currency {
add(number: currency.Any): currency;
subtract(number: currency.Any): currency;
multiply(number: currency.Any): currency;
divide(number: currency.Any): currency;
distribute(count: number): Array<currency>;
dollars(): number;
cents(): number;
format(opts?: currency.Options | currency.Format): string;
toString(): string;
toJSON(): number;
readonly intValue: number;
readonly value: number;
}

declare const currency : currency.Constructor;
const currency: currency.Constructor;

declare module 'currency.js' {
export = currency;
}

0 comments on commit 4a7946a

Please sign in to comment.