Skip to content

Make BaseTarget parameters getters stricter #1534

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

Merged
merged 1 commit into from
Jun 17, 2023

Conversation

auscompgeek
Copy link
Member

@auscompgeek auscompgeek commented Jun 15, 2023

Context

#1520 (comment)

What

This adds a generic to BaseTarget, which is expected to receive the type of the subclass' parameters. This tries its best to enforce that is the case, and ensures implementations of getCloneParameters return that declared type.

Checklist

@auscompgeek auscompgeek requested a review from pokey as a code owner June 15, 2023 14:58
@@ -39,7 +51,7 @@ export default abstract class BaseTarget implements Target {
isNotebookCell = false;
isWord = false;

constructor(parameters: CommonTargetParameters) {
constructor(parameters: TParameters & CommonTargetParameters) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This ensures that whatever type is declared actually ends up existing in the subclass constructor somehow.

Copy link
Member

@pokey pokey Jun 15, 2023

Choose a reason for hiding this comment

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

oh really? so the type signature of the base class constructor influences the type signature of derived class constructors? That's surprising to me. And I'm not sure how that works in the case where the derived class uses minimum parameters instead of common parameters

return {
...this.state,
leadingDelimiterRange: this.leadingDelimiterRange_,
trailingDelimiterRange: this.trailingDelimiterRange_,
insertionDelimiter: this.insertionDelimiter,
Copy link
Member Author

Choose a reason for hiding this comment

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

All this ceremony wasn't for nothing, it seems! I added the return type here for autocomplete.

Copy link
Member

@pokey pokey Jun 15, 2023

Choose a reason for hiding this comment

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

So you don't get autocomplete for free from the generic at the top / base class function? Not that I'm opposed to extra return type annotations 🤷‍♂️

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

this is great. left a couple questions inline mostly for my own education 😄

I'm not totally in love with needing to specify that generic parameter, when you'd think TS would infer it from some function arg or something, but it doesn't, so I don't think we have a choice

btw elsewhere, I've made it so that it extracts what it needs from the type itself, and you just pass the type in as the only generic. we may consider going down that road if we have more things we need to infer. See eg

class StartPosition extends QueryPredicateOperator<StartPosition> {

* @template TParameters The constructor parameters.
*/
export default abstract class BaseTarget<
in out TParameters extends MinimumTargetParameters,
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why you used in out here?

@pokey pokey added this pull request to the merge queue Jun 17, 2023
Merged via the queue into main with commit a8ea10d Jun 17, 2023
@pokey pokey deleted the auscompgeek/stricter-target-param branch June 17, 2023 15:20
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