Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TS declaration declares currency global (#297)
* 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
Showing
1 changed file
with
36 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |