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

refactor: enable type checks inside mixin classes #5394

Merged
merged 1 commit into from
May 14, 2020

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented May 12, 2020

Introduce a new type helper MixinTarget allowing mixin functions to accept a type that describes public members of the target class only. This is working around the current TypeScript limitations:

Rework all existing mixins and the related documentation to use MixinTarget<RealClass> instead of Constructor<any>.

Fix any errors discovered by the compiler after enabling type checks.

See also microsoft/TypeScript#38496

This PR depends on loopbackio/loopback-datasource-juggler#1839, the build will keep failing until a new version of juggler is released & loopback-next package locks are updated.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

Copy link
Member Author

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

Here are places where our implementation contained a bug that was discovered after type checks were enabled 👇

@@ -57,26 +48,42 @@ export function BootMixin<T extends Constructor<any>>(superClass: T) {
() => this.projectRoot,
);
this.bind(BootBindings.BOOT_OPTIONS).toDynamicValue(
() => this.bootOptions,
() => this.bootOptions ?? {},
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 problem was hidden by any before.

this.mountComponentBooters(component);
return binding;
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 problem was hidden by any before.

this.mountComponentRepositories(component);
const binding = super.component(componentCtor, nameOrOptions);
this.mountComponentRepositories(componentCtor);
return binding;
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 problem was hidden by any before.

@bajtos bajtos force-pushed the refactor/better-mixin-types branch from 0186756 to 4c03819 Compare May 12, 2020 09:19
Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

💯 👍

@bajtos bajtos force-pushed the refactor/better-mixin-types branch from 4c03819 to f1e862c Compare May 12, 2020 16:22
Introduce a new type helper `MixinTarget` allowing mixin functions
to accept a type that describes public members of the target class
only. This is working around the current TypeScript limitations.

Rework all existing mixins and the related documentation to use
`MixinTarget<RealClass>` instead of `Constructor<any>`.

Fix any errors discovered by the compiler after enabling type checks.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
@bajtos bajtos force-pushed the refactor/better-mixin-types branch from f1e862c to d8bcc88 Compare May 14, 2020 07:36
@bajtos bajtos merged commit 6cea13d into master May 14, 2020
@bajtos bajtos deleted the refactor/better-mixin-types branch May 14, 2020 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants