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

Fix Schema's clone function typing #10951

Merged
merged 3 commits into from
Nov 6, 2021
Merged

Conversation

StefanoA1
Copy link
Contributor

Summary
The Schema's clone function does not preserve the typing (and on its best case, it loses the name of the type and just types the cloned schema as a nameless object)

Examples
Before:
clone returns an untyped schema (or at best it returns an object but the type name/interface/class is lost)

After:
clone returns a typed schema which can be the original type or a type that extends the base type (would work for type extending it with partial params)

Before:
clone returns an untyped schema

After:
clone returns a typed schema which can be the original type or a type that extends the base type (would work for type extending it with partial params)
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

I'm supportive of adding a generic param to clone(), but I'm not sure making clone() return something other than this is the right call. For one thing, you're missing a bunch of generic parameters to Schema. And I can confirm that this respects generics

Can't we do clone<T = this>(): T?

@StefanoA1
Copy link
Contributor Author

StefanoA1 commented Nov 4, 2021

I'm supportive of adding a generic param to clone(), but I'm not sure making clone() return something other than this is the right call. For one thing, you're missing a bunch of generic parameters to Schema. And I can confirm that this respects generics

Can't we do clone<T = this>(): T?

I agree, but then T should ensure to extend Schema<DocType> at least (to ensure that not any type is taken as a generic here), what are your thoughts on:
clone<T extends Schema<DocType> = this>(): T;
then ?

@vkarpov15
Copy link
Collaborator

Unfortunately not. 2 reasons: 1) T extends Schema<DocType> is missing a bunch of generic parameters, and 2) extends sometimes has some serious performance issues, see #10349 .

@vkarpov15 vkarpov15 merged commit 85a56ea into Automattic:master Nov 6, 2021
@vkarpov15 vkarpov15 added this to the 6.0.13 milestone Nov 6, 2021
@StefanoA1
Copy link
Contributor Author

@vkarpov15 ah! interesting, do you happen to have any docs on why extends causes such perf issues? thanks for the link

@StefanoA1 StefanoA1 deleted the patch-2 branch November 8, 2021 09:58
@vkarpov15
Copy link
Collaborator

Wish I knew, but I'm not experienced enough with TS internals to know why sometimes extends in generics is such a perf issue.

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.

None yet

2 participants