diff --git a/__tests__/genType.test.js b/__tests__/genType.test.js index 573378d..883949b 100644 --- a/__tests__/genType.test.js +++ b/__tests__/genType.test.js @@ -6,7 +6,7 @@ it('getResourceTypeReference converts a resource path to a valid reference', () it('getNewKeyTypeFromBatchKeySetType returns a newKey type with a valid value', () => { expect(getNewKeyTypeFromBatchKeySetType('bKey', "ResourcesType['foo']['bar']['baz']")).toBe( - `Parameters[0]`, + `GetSetType`, ); }); diff --git a/examples/swapi/swapi-loaders.ts b/examples/swapi/swapi-loaders.ts index b78f64e..177ed8f 100644 --- a/examples/swapi/swapi-loaders.ts +++ b/examples/swapi/swapi-loaders.ts @@ -55,6 +55,8 @@ export type DataLoaderCodegenOptions = { }; }; +type GetSetType = T extends Set ? U : never; + /** * =============================== * BEGIN: printResourcesType() @@ -101,9 +103,7 @@ export type LoadersType = Readonly<{ >; getFilms: DataLoader< Omit[0], "film_ids"> & { - film_id: Parameters< - Parameters[0]["film_ids"]["has"] - >[0]; + film_id: GetSetType[0]["film_ids"]>; }, PromisedReturnType[0], // This third argument is the cache key type. Since we use objectHash in cacheKeyOptions, this is "string". @@ -905,9 +905,9 @@ export default function getLoaders( ), getFilms: new DataLoader< Omit[0], "film_ids"> & { - film_id: Parameters< - Parameters[0]["film_ids"]["has"] - >[0]; + film_id: GetSetType< + Parameters[0]["film_ids"] + >; }, PromisedReturnType[0], // This third argument is the cache key type. Since we use objectHash in cacheKeyOptions, this is "string". diff --git a/src/codegen.ts b/src/codegen.ts index 72297b8..7d50c64 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -97,6 +97,8 @@ export default function codegen( }; }; + type GetSetType = T extends Set ? U : never; + /** * =============================== * BEGIN: printResourcesType() diff --git a/src/genType.ts b/src/genType.ts index 07aa885..bd9545a 100644 --- a/src/genType.ts +++ b/src/genType.ts @@ -15,11 +15,10 @@ function getResourceArg(resourceConfig: ResourceConfig, resourcePath: ReadonlyAr } /** - * Extract the type T from a Set resource (in this case a batchKey's resource) - * using its `.has(T)`'s function paremeter type + * Extract the type T from a Set resource (using the helper defined in codegen.ts) */ export function getNewKeyTypeFromBatchKeySetType(batchKey: string, resourceArgs: string) { - return `Parameters<${resourceArgs}['${batchKey}']['has']>[0]`; + return `GetSetType<${resourceArgs}['${batchKey}']>`; } export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray) {