Skip to content

Commit

Permalink
fix: TypeScript 5.7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Dec 28, 2024
1 parent 4a2ebe1 commit 39e318a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
26 changes: 26 additions & 0 deletions .changeset/perfect-walls-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@data-client/rest': patch
---

Resource.extend() compatibility with TypeScript 5

Previously [extending existing members](https://dataclient.io/rest/api/resource#extend-override) with no
typed overrides (like [path](https://dataclient.io/rest/api/resource#path)) would not work starting with
TypeScript 5.7.

```ts
const UserResource = UserResourceBase.extend({
partialUpdate: {
getOptimisticResponse(snap, params, body) {
params.id;
params.group;
// @ts-expect-error
params.nothere;
return {
id: params.id,
...body,
};
},
},
});
```
10 changes: 5 additions & 5 deletions packages/rest/src/resourceExtendable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export interface Extendable<
};
extend<
R extends ResourceInterface,
Get extends PartialRestGenerics = any,
GetList extends PartialRestGenerics = any,
Update extends PartialRestGenerics = any,
PartialUpdate extends PartialRestGenerics = any,
Delete extends PartialRestGenerics = any,
Get extends PartialRestGenerics = {},
GetList extends PartialRestGenerics = {},
Update extends PartialRestGenerics = {},
PartialUpdate extends PartialRestGenerics = {},
Delete extends PartialRestGenerics = {},
>(
this: R,
options: ResourceEndpointExtensions<
Expand Down
10 changes: 5 additions & 5 deletions packages/rest/src/resourceExtensionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export type ExtendedResource<

export interface ResourceEndpointExtensions<
R extends ResourceInterface,
Get extends PartialRestGenerics = any,
GetList extends PartialRestGenerics = any,
Update extends PartialRestGenerics = any,
PartialUpdate extends PartialRestGenerics = any,
Delete extends PartialRestGenerics = any,
Get extends PartialRestGenerics = {},
GetList extends PartialRestGenerics = {},
Update extends PartialRestGenerics = {},
PartialUpdate extends PartialRestGenerics = {},
Delete extends PartialRestGenerics = {},
> {
readonly get?: RestEndpointOptions<
unknown extends Get ? EndpointToFunction<R['get']>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ interface CustomResource<R extends ResourceInterface, O extends ResourceGenerics
delete: unknown extends Delete ? R['delete'] : PartialRestGenerics extends Delete ? R['delete'] : RestExtendedEndpoint<Delete, R['delete']>;
}
type ExtendedResource<R extends ResourceInterface, T extends Record<string, EndpointInterface>> = Omit<R, keyof T> & T;
interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any> {
interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = {}, GetList extends PartialRestGenerics = {}, Update extends PartialRestGenerics = {}, PartialUpdate extends PartialRestGenerics = {}, Delete extends PartialRestGenerics = {}> {
readonly get?: RestEndpointOptions<unknown extends Get ? EndpointToFunction<R['get']> : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>, R['get']['schema']> & Readonly<Get>;
readonly getList?: RestEndpointOptions<unknown extends GetList ? EndpointToFunction<R['getList']> : OptionsToFunction<GetList, R['getList'], EndpointToFunction<R['getList']>>, R['getList']['schema']> & Readonly<GetList>;
readonly update?: RestEndpointOptions<unknown extends Update ? EndpointToFunction<R['update']> : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>, R['update']['schema']> & Readonly<Update>;
Expand All @@ -1571,7 +1571,7 @@ interface Extendable<O extends ResourceGenerics = {
}, const ExtendKey extends string, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R['get'], EndpointToFunction<R['get']>> & ExtendOptions>): R & {
[key in ExtendKey]: RestExtendedEndpoint<ExtendOptions, R['get']>;
};
extend<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
extend<R extends ResourceInterface, Get extends PartialRestGenerics = {}, GetList extends PartialRestGenerics = {}, Update extends PartialRestGenerics = {}, PartialUpdate extends PartialRestGenerics = {}, Delete extends PartialRestGenerics = {}>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
extend<R extends ResourceInterface, T extends Record<string, EndpointInterface>>(this: R, extender: (baseResource: R) => T): ExtendedResource<R, T>;
}

Expand Down
4 changes: 2 additions & 2 deletions website/src/components/Playground/editor-types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ interface CustomResource<R extends ResourceInterface, O extends ResourceGenerics
delete: unknown extends Delete ? R['delete'] : PartialRestGenerics extends Delete ? R['delete'] : RestExtendedEndpoint<Delete, R['delete']>;
}
type ExtendedResource<R extends ResourceInterface, T extends Record<string, EndpointInterface>> = Omit<R, keyof T> & T;
interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any> {
interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = {}, GetList extends PartialRestGenerics = {}, Update extends PartialRestGenerics = {}, PartialUpdate extends PartialRestGenerics = {}, Delete extends PartialRestGenerics = {}> {
readonly get?: RestEndpointOptions<unknown extends Get ? EndpointToFunction<R['get']> : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>, R['get']['schema']> & Readonly<Get>;
readonly getList?: RestEndpointOptions<unknown extends GetList ? EndpointToFunction<R['getList']> : OptionsToFunction<GetList, R['getList'], EndpointToFunction<R['getList']>>, R['getList']['schema']> & Readonly<GetList>;
readonly update?: RestEndpointOptions<unknown extends Update ? EndpointToFunction<R['update']> : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>, R['update']['schema']> & Readonly<Update>;
Expand All @@ -1575,7 +1575,7 @@ interface Extendable<O extends ResourceGenerics = {
}, const ExtendKey extends string, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R['get'], EndpointToFunction<R['get']>> & ExtendOptions>): R & {
[key in ExtendKey]: RestExtendedEndpoint<ExtendOptions, R['get']>;
};
extend<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
extend<R extends ResourceInterface, Get extends PartialRestGenerics = {}, GetList extends PartialRestGenerics = {}, Update extends PartialRestGenerics = {}, PartialUpdate extends PartialRestGenerics = {}, Delete extends PartialRestGenerics = {}>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
extend<R extends ResourceInterface, T extends Record<string, EndpointInterface>>(this: R, extender: (baseResource: R) => T): ExtendedResource<R, T>;
}

Expand Down

0 comments on commit 39e318a

Please sign in to comment.