Browse Source

fix(server): Correctly type the pick function

Michael Bromley 6 years ago
parent
commit
b1dc203ccf
2 changed files with 4 additions and 7 deletions
  1. 2 5
      server/src/service/services/collection.service.ts
  2. 2 2
      shared/pick.ts

+ 2 - 5
server/src/service/services/collection.service.ts

@@ -132,12 +132,9 @@ export class CollectionService implements OnModuleInit {
         if (idsAreEqual(collection.id, rootCollection.id)) {
             return [pick(rootCollection, ['id', 'name'])];
         }
+        const pickProps = pick(['id', 'name']);
         const ancestors = await this.getAncestors(collection.id, ctx);
-        return [
-            pick(rootCollection, ['id', 'name']),
-            ...ancestors.map(pick(['id', 'name'])),
-            pick(collection, ['id', 'name']),
-        ];
+        return [pickProps(rootCollection), ...ancestors.map(pickProps), pickProps(collection)];
     }
 
     async getCollectionsByProductId(

+ 2 - 2
shared/pick.ts

@@ -3,8 +3,8 @@
  * 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 object, U extends T>(props: Array<keyof T>): (input: U) => Pick<U, keyof T>;
-export function pick<T extends object, U extends T = any>(input: U, props: Array<keyof T>): Pick<U, keyof T>;
+export function pick<T extends string>(props: T[]): <U>(input: U) => { [K in T]: K extends keyof U ? U[K]: never };
+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>,