From 33e4ecd2fc5ad18f8d3017e47cdeea7cd2d78d05 Mon Sep 17 00:00:00 2001 From: Kyle Chine <38026585+kylechine@users.noreply.github.com> Date: Sat, 2 Nov 2019 03:02:41 +1100 Subject: [PATCH] Bugfix: Change the Export Method of UIDGenerator Class from Named-Export to Default-Export - @types/uid-generator (#39672) * bugfix: class should be exported default * bugfix: test failure * temp * also export interfaces * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * syntax error fix * index.d.ts done * Bugfix: Change the Export Method of UIDGenerator Class from Named-Export to Default-Export Motivation In the original package (uid-generator), the class is exported by Default-Export: module.exports = UIDGenerator; But in the current version of the type definition, the class is exported by Named-Export: export const UIDGenerator: UIDGeneratorConstructor; Which leads to a piece of wrong JavaScript code after compilation: const uid_generator_1 = require("uid-generator"); const uidgen = new uid_generator_1.UIDGenerator(bitLength); The correct piece should be: const uid_generator_1 = require("uid-generator"); const uidgen = new uid_generator_1(bitLength); So I feel it may be necessary to fix this. Fixed List 1. Interface Renaming UIDGenerator -> UIDGeneratorInstance UIDGeneratorConstructor -> UIDGeneratorClass In the current version, the name UIDGenerator used as both the name of the interface and the class, which may leads to confusion. So I give them a little bit more clear names. 2. Default-Export UIDGenerator 3. UIDGeneratorInstance.generate(call-back): Error Parameter Added In the orginal (uid-generator) package, the call-back function also accepts an error parameter. And when error did triggered, uid parameter may be optional. 4. Corresponding updates in test script * syntax fix * add target es2015 * Update types/uid-generator/tsconfig.json Remove useless spaces. Co-Authored-By: Emrio * ES2015 to es6 * Restore to last time test-passed "target": "ES2015", * Remove extra spaces "target": "es6" * Update types/uid-generator/index.d.ts Remove the ? from uid parameter. Co-Authored-By: Emrio * Update types/uid-generator/uid-generator-tests.ts uid must be string now. Co-Authored-By: Emrio * uid required now * Remove target option * add target:es6 --- types/uid-generator/index.d.ts | 15 +++++++++------ types/uid-generator/tsconfig.json | 1 + types/uid-generator/uid-generator-tests.ts | 11 +++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/types/uid-generator/index.d.ts b/types/uid-generator/index.d.ts index 01850e55ab..a27f808bc5 100644 --- a/types/uid-generator/index.d.ts +++ b/types/uid-generator/index.d.ts @@ -1,21 +1,22 @@ // Type definitions for uid-generator 2.0 // Project: https://github.com/nwoltman/node-uid-generator // Definitions by: TheEmrio +// KyleChine // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -export interface UIDGenerator { +interface UIDGeneratorInstance { readonly bitSize: number; readonly uidLength: number; readonly baseEncoding: string; readonly base: number; generateSync(): string; generate(): Promise; - generate(cb: (uid: string) => any): void; + generate(cb: (error: Error|null, uid: string) => any): void; } -export interface UIDGeneratorConstructor { - new (bitSize?: number, baseEncoding?: string): UIDGenerator; - new (baseEncoding?: string): UIDGenerator; +interface UIDGeneratorClass { + new (bitSize?: number, baseEncoding?: string): UIDGeneratorInstance; + new (baseEncoding?: string): UIDGeneratorInstance; readonly BASE16: '0123456789abcdef'; readonly BASE36: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; readonly BASE58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; @@ -25,4 +26,6 @@ export interface UIDGeneratorConstructor { readonly BASE94: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; } -export const UIDGenerator: UIDGeneratorConstructor; +declare const UIDGenerator: UIDGeneratorClass; + +export { UIDGenerator as default, UIDGeneratorClass, UIDGeneratorInstance }; diff --git a/types/uid-generator/tsconfig.json b/types/uid-generator/tsconfig.json index dabe9d7be2..4465512954 100644 --- a/types/uid-generator/tsconfig.json +++ b/types/uid-generator/tsconfig.json @@ -4,6 +4,7 @@ "lib": [ "es6" ], + "target": "es6", "noImplicitAny": true, "noImplicitThis": true, "strictFunctionTypes": true, diff --git a/types/uid-generator/uid-generator-tests.ts b/types/uid-generator/uid-generator-tests.ts index c2d4e8b7fc..3459feb5f7 100644 --- a/types/uid-generator/uid-generator-tests.ts +++ b/types/uid-generator/uid-generator-tests.ts @@ -1,14 +1,17 @@ -import { UIDGenerator } from 'uid-generator'; +import { default as UIDGenerator } from 'uid-generator'; -new UIDGenerator('abc'); // $ExpectType UIDGenerator -const generator = new UIDGenerator(128, 'abc'); // $ExpectType UIDGenerator +new UIDGenerator('abc'); // $ExpectType UIDGeneratorInstance +const generator = new UIDGenerator(128, 'abc'); // $ExpectType UIDGeneratorInstance generator.generateSync(); // $ExpectType string -generator.generate(uid => { +generator.generate((err, uid) => { + err; // $ExpectType Error | null uid; // $ExpectType string }); generator.generate().then(uid => { uid; // $ExpectType string +}).catch(e => { + e; // $ExpectType any }); generator.bitSize; // $ExpectType number