Browse Source

fix(server): Improve type def of pick function

Michael Bromley 6 years ago
parent
commit
1e7c3745d5
1 changed files with 7 additions and 7 deletions
  1. 7 7
      shared/pick.ts

+ 7 - 7
shared/pick.ts

@@ -1,14 +1,14 @@
 /**
- * Returns a new object which is a subject of the input, including only the specified properties.
+ * Returns a new object which is a subset of the input, including only the specified properties.
  * Can be called with a single argument (array of props to pick), in which case it returns a partially
  * applied pick function.
  */
-export function pick<T extends string>(props: T[]): <U>(input: U) => { [K in T]: K extends keyof U ? U[K]: never };
+export function pick<T extends string>(props: T[]): <U>(input: U) => Pick<U, Extract<keyof U, T>>;
 export function pick<U, T extends keyof U>(input: U, props: T[]): { [K in T]: U[K] };
-export function pick<T extends object, U extends T = any>(
-    inputOrProps: U | Array<keyof T>,
-    maybeProps?: Array<keyof T>,
-): T | ((input: U) => T) {
+export function pick<U, T extends keyof U>(
+    inputOrProps: U | T[],
+    maybeProps?: T[],
+): { [K in T]: U[K] } | ((input: U) => Pick<U, Extract<keyof U, T>>) {
     if (Array.isArray(inputOrProps)) {
         return (input: U) => _pick(input, inputOrProps);
     } else {
@@ -16,7 +16,7 @@ export function pick<T extends object, U extends T = any>(
     }
 }
 
-function _pick<T extends object, U extends T = any>(input: U, props: Array<keyof T>): T {
+function _pick<U, T extends keyof U>(input: U, props: T[]): { [K in T]: U[K] } {
     const output: any = {};
     for (const prop of props) {
         output[prop] = input[prop];