From 4b589cecf47972002875ee30036cbbdb6dafd55a Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Wed, 22 May 2019 18:45:00 +0200 Subject: [PATCH] [sha-1] Strongly type the output using overloads. (#35645) See options doc at https://github.com/pvorb/node-sha1. --- types/sha1/index.d.ts | 13 +++++++++++-- types/sha1/sha1-tests.ts | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/types/sha1/index.d.ts b/types/sha1/index.d.ts index bb55d2f4fc..b65c456054 100644 --- a/types/sha1/index.d.ts +++ b/types/sha1/index.d.ts @@ -12,10 +12,19 @@ * @param options - an options object * @returns the resultant SHA1 hash of the given message */ +declare function main(message: string | Buffer, options?: Sha1AsStringOptions): string; +declare function main(message: string | Buffer, options?: Sha1AsBytesOptions): Uint8Array; declare function main(message: string | Buffer, options?: Sha1Options): string | Uint8Array; export = main; -interface Sha1Options { - asBytes?: boolean; +interface Sha1AsStringOptions { + asBytes?: false; asString?: boolean; } + +interface Sha1AsBytesOptions { + asBytes: true; + asString?: false; +} + +type Sha1Options = Sha1AsStringOptions | Sha1AsBytesOptions; diff --git a/types/sha1/sha1-tests.ts b/types/sha1/sha1-tests.ts index 6ce833b10b..12feb5b3d8 100644 --- a/types/sha1/sha1-tests.ts +++ b/types/sha1/sha1-tests.ts @@ -9,3 +9,7 @@ const testThree = sha1('message', {asBytes: true}); fs.readFile('sha1.d.ts', (err: Error, buf: Buffer) => { console.log(sha1(buf)); }); + +const asHexString: string = sha1('message'); +const asBinString: string = sha1('message', {asString: true}); +const asBytes: Uint8Array = sha1('message', {asBytes: true});