[sha-1] Strongly type the output using overloads. (#35645)

See options doc at https://github.com/pvorb/node-sha1.
This commit is contained in:
Pascal Corpet
2019-05-22 18:45:00 +02:00
committed by Ryan Cavanaugh
parent 11994525b0
commit 4b589cecf4
2 changed files with 15 additions and 2 deletions

13
types/sha1/index.d.ts vendored
View File

@@ -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;

View File

@@ -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});