From 2e33b3ac79d0e3d296dd58c473e87f4634dc27e5 Mon Sep 17 00:00:00 2001 From: Kyler Jensen <15389109+kylerjensen@users.noreply.github.com> Date: Tue, 15 Jun 2021 12:48:50 -0600 Subject: [PATCH] feat: improve typescript definition (#28) This commit improves the typescript definition of `hashValue()` by introducing a generic type argument and an explicit return type. This ensures that the inferred return type of the function matches the type of values in the provided `options` array. Before: ```ts const options1 = [1, 2, 3]; const options2 = ['a', 'b', 'c']; const options3 = ['a', 'b', 'c', 1, 2, 3]; const value1 = hashValue('str', options1); // tsc: type of `value1` is `unknown` const value2 = hashValue('str', options2); // tsc: type of `value2` is `unknown` const value3 = hashValue('str', options3); // tsc: type of `value3` is `unknown` ``` After: ```ts const options1 = [1, 2, 3]; const options2 = ['a', 'b', 'c']; const options3 = ['a', 'b', 'c', 1, 2, 3]; const value1 = hashValue('str', options1); // tsc: type of `value1` is `number` const value2 = hashValue('str', options2); // tsc: type of `value2` is `string` const value3 = hashValue('str', options3); // tsc: type of `value3` is `string | number` ``` --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2f7f8f6..4d1539f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ * @param source String to hash. * @param options Collection of options to get value from. */ -export default function hashValue(source: string, options: unknown[]) { +export default function hashValue(source: string, options: T[]): T { let hash = 0; if (source.length === 0 || options.length === 0) {