Skip to content

Commit 9932d41

Browse files
authored
Merge pull request #356 from Yelp/fix-set-key-type
Fix Set key type for isBatchKeyASet + ts-reset
2 parents cff211e + 3082314 commit 9932d41

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

__tests__/genType.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ it('getResourceTypeReference converts a resource path to a valid reference', ()
66

77
it('getNewKeyTypeFromBatchKeySetType returns a newKey type with a valid value', () => {
88
expect(getNewKeyTypeFromBatchKeySetType('bKey', "ResourcesType['foo']['bar']['baz']")).toBe(
9-
`Parameters<ResourcesType['foo']['bar']['baz']['bKey']['has']>[0]`,
9+
`GetSetType<ResourcesType['foo']['bar']['baz']['bKey']>`,
1010
);
1111
});
1212

examples/swapi/swapi-loaders.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export type DataLoaderCodegenOptions = {
5555
};
5656
};
5757

58+
type GetSetType<T> = T extends Set<infer U> ? U : never;
59+
5860
/**
5961
* ===============================
6062
* BEGIN: printResourcesType()
@@ -101,9 +103,7 @@ export type LoadersType = Readonly<{
101103
>;
102104
getFilms: DataLoader<
103105
Omit<Parameters<ResourcesType["getFilms"]>[0], "film_ids"> & {
104-
film_id: Parameters<
105-
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]["has"]
106-
>[0];
106+
film_id: GetSetType<Parameters<ResourcesType["getFilms"]>[0]["film_ids"]>;
107107
},
108108
PromisedReturnType<ResourcesType["getFilms"]>[0],
109109
// 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(
905905
),
906906
getFilms: new DataLoader<
907907
Omit<Parameters<ResourcesType["getFilms"]>[0], "film_ids"> & {
908-
film_id: Parameters<
909-
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]["has"]
910-
>[0];
908+
film_id: GetSetType<
909+
Parameters<ResourcesType["getFilms"]>[0]["film_ids"]
910+
>;
911911
},
912912
PromisedReturnType<ResourcesType["getFilms"]>[0],
913913
// This third argument is the cache key type. Since we use objectHash in cacheKeyOptions, this is "string".

src/codegen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export default function codegen(
9797
};
9898
};
9999
100+
type GetSetType<T> = T extends Set<infer U> ? U : never;
101+
100102
/**
101103
* ===============================
102104
* BEGIN: printResourcesType()

src/genType.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ function getResourceArg(resourceConfig: ResourceConfig, resourcePath: ReadonlyAr
1515
}
1616

1717
/**
18-
* Extract the type T from a Set<T> resource (in this case a batchKey's resource)
19-
* using its `.has(T)`'s function paremeter type
18+
* Extract the type T from a Set<T> resource (using the helper defined in codegen.ts)
2019
*/
2120
export function getNewKeyTypeFromBatchKeySetType(batchKey: string, resourceArgs: string) {
22-
return `Parameters<${resourceArgs}['${batchKey}']['has']>[0]`;
21+
return `GetSetType<${resourceArgs}['${batchKey}']>`;
2322
}
2423

2524
export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>) {

0 commit comments

Comments
 (0)