Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typings of reverse navigation properties #1456

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Page-
Copy link
Contributor

@Page- Page- commented Oct 28, 2024

Change-type: patch

Resolves: #
HQ:
See:
Depends-on:
Change-type: major|minor|patch


Contributor checklist
  • Includes tests
  • Includes typings
  • Includes updated documentation
  • Includes updated build output

@Page- Page- requested a review from thgreasi October 28, 2024 14:58
@@ -97,7 +97,7 @@ const getServiceModel = ({
const { service } = await sdkInstance.models.application.get(
slugOrUuidOrId,
{
$select: 'service',
$select: 'id',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typings always attempted to block selecting reverse navigation resources but didn't work quite right, with the fixes it now blocks the select attempt as expected

@@ -87,14 +87,14 @@ export interface Organization {
is_using__billing_version: 'v1' | 'v2';
logo_image: WebResource;

application: ReverseNavigationResource<Application>;
application?: ReverseNavigationResource<Application>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made all the reverse navigation props optional as the improved typings now require that since they are usually omitted (ie whenever they aren't explicitly expanded)

@@ -95,14 +102,14 @@ type ExpandedProperty<
: never;

export type ExpandResultObject<T, Props extends keyof T> = {
[P in Props]: ExpandedProperty<T, P, object>;
[P in Props]-?: ExpandedProperty<T, P, object>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the reverse navigation properties are always optional this removes their optionality for when they are expanded

};

type ExpandResourceExpandObject<
T,
TResourceExpand extends ResourceExpand<T>,
> = {
[P in keyof TResourceExpand]: ExpandedProperty<
[P in keyof TResourceExpand]-?: ExpandedProperty<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the reverse navigation properties are always optional this removes their optionality for when they are expanded

@@ -3,11 +3,11 @@ export interface AnyObject {
}

export type PropsOfType<T, P> = {
[K in keyof T]: T[K] extends P ? K : never;
[K in keyof T]-?: T[K] extends P ? K : never;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes returning 'a' | 'b' | undefined when any of the props are optional

}[keyof T];

export type PropsAssignableWithType<T, P> = {
[K in keyof T]: P extends T[K] ? K : never;
[K in keyof T]-?: P extends T[K] ? K : never;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes returning 'a' | 'b' | undefined when any of the props are optional

@@ -46,7 +46,14 @@ export type InferAssociatedResourceType<T> =
export type SelectableProps<T> =
// This allows us to get proper results when T is any/AnyObject, otherwise this returned never
PropsOfType<T, ReverseNavigationResource<object>> extends StringKeyof<T>
? StringKeyof<T>
? StringKeyof<T> extends PropsOfType<T, ReverseNavigationResource<object>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary to keep things working nicely now that PropsOfType no longer incorrectly returns 'a' | 'b' | undefined when there are optional properties, meaning that PropsOfType<T, ReverseNavigationResource<object>> extends StringKeyof<T> now matches (previously it failed due to the undefined)

@@ -250,7 +250,7 @@ await (async () => {
options: {
$select: ['id', 'device_name'],
},
});
} as const);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect that this is going to be an intrusive change for TS consumers to do in a patch :(
Especially since I don't expect adding as const to be obvious to users.
I will try to find a workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I'm not sure it's worth trying to fix @thgreasi, I think the simple fact might be that in order to make these typings work without breaking changes just might not be possible since I think we might well hit an issue where it relies on bugs that force an any and it is not fixable without as const (or some brand new version of typescript or otherwise some change that is not actually backwards compatible. So overall I think it'd make more sense to just eat the major and switch to using the pinejs-client-js typings which have been used more extensively at this point and were built with the benefits of modern typescript from the beginning.

Aside from that I no longer need this PR to acheive my original aims of using the auto-generated typings since I was able to avoid hitting the bug whilst also improving typings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants