Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/genType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourcesType['foo']['bar']['baz']['bKey']['has']>[0]`,
`GetSetType<ResourcesType['foo']['bar']['baz']['bKey']>`,
);
});

Expand Down
12 changes: 6 additions & 6 deletions examples/swapi/swapi-loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export type DataLoaderCodegenOptions = {
};
};

type GetSetType<T> = T extends Set<infer U> ? U : never;

/**
* ===============================
* BEGIN: printResourcesType()
Expand Down Expand Up @@ -101,9 +103,7 @@ export type LoadersType = Readonly<{
>;
getFilms: DataLoader<
Omit<Parameters<ResourcesType["getFilms"]>[0], "film_ids"> & {
film_id: Parameters<
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]["has"]
>[0];
film_id: GetSetType<Parameters<ResourcesType["getFilms"]>[0]["film_ids"]>;
},
PromisedReturnType<ResourcesType["getFilms"]>[0],
// This third argument is the cache key type. Since we use objectHash in cacheKeyOptions, this is "string".
Expand Down Expand Up @@ -905,9 +905,9 @@ export default function getLoaders(
),
getFilms: new DataLoader<
Omit<Parameters<ResourcesType["getFilms"]>[0], "film_ids"> & {
film_id: Parameters<
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]["has"]
>[0];
film_id: GetSetType<
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]
>;
},
PromisedReturnType<ResourcesType["getFilms"]>[0],
// This third argument is the cache key type. Since we use objectHash in cacheKeyOptions, this is "string".
Expand Down
2 changes: 2 additions & 0 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default function codegen(
};
};

type GetSetType<T> = T extends Set<infer U> ? U : never;

/**
* ===============================
* BEGIN: printResourcesType()
Expand Down
5 changes: 2 additions & 3 deletions src/genType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ function getResourceArg(resourceConfig: ResourceConfig, resourcePath: ReadonlyAr
}

/**
* Extract the type T from a Set<T> resource (in this case a batchKey's resource)
* using its `.has(T)`'s function paremeter type
* Extract the type T from a Set<T> 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<string>) {
Expand Down