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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

repository: Add `defineModelClass` helper and fix `DeepPartial<AnyObject>` #4266

Merged
merged 2 commits into from Dec 9, 2019

Conversation

@bajtos
Copy link
Member

bajtos commented Dec 6, 2019

See #4235 for background context.

Implement a new helper function that allows LB4 users to create
model classes dynamically at runtime. This is similar functionality
as offered by LB3, but with the benefit of compile-time type checks.

Example use:

const definition = new ModelDefinition('Product')
  .addProperty('id', {type: 'number', id: true})
  .addProperty('name', {
    type: 'string',
  });

const Product = defineModelClass<
  typeof Entity,
  {id: number; name: string}
>(
  Entity,
  definition,
);

While testing this code, I found a bug in DeepPartial<T> that I have experienced before. This time I could not ignore it because one of my tests would fail, so I am finally fixing it (see the first commit). Consider the following code:

function create<T extends object>(data?: DeepPartial<T>) {
  // ...
}

Before this change, create<AnyObject>({key: 'value'}) is rejected by the compiler, becase "No overload matches this call."

With this change in place, the compiler no longer complains.

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 馃憟

bajtos added 2 commits Dec 6, 2019
Consider the following code:

  function create<T extends object>(data?: DeepPartial<T>) {
    // ...
  }

Before this change, `create<AnyObject>({key: 'value'})` is rejected
by the compiler, becase "No overload matches this call."

With this change in place, the compiler no longer complains.

Signed-off-by: Miroslav Bajto拧 <mbajtoss@gmail.com>
Implement a new helper function that allows LB4 users to create
model classes dynamically at runtime. This is similar functionality
as offered by LB3, but with the benefit of compile-time type checks.

Example use:

    const definition = new ModelDefinition('Product')
      .addProperty('id', {type: 'number', id: true})
      .addProperty('name', {
        type: 'string',
      });

    const Product = defineModelClass<
      typeof Entity,
      {id: number; name: string}
    >(
      Entity,
      definition,
    );

Signed-off-by: Miroslav Bajto拧 <mbajtoss@gmail.com>
@bajtos bajtos added this to the Dec 2019 milestone Dec 6, 2019
@bajtos bajtos requested review from raymondfeng and strongloop/loopback-maintainers Dec 6, 2019
@bajtos bajtos self-assigned this Dec 6, 2019
@bajtos bajtos mentioned this pull request Dec 6, 2019
2 of 2 tasks complete
@bajtos bajtos changed the title repository: Add `defineModelClass` helper and fix DeepPartial<AnyObject> repository: Add `defineModelClass` helper and fix `DeepPartial<AnyObject>` Dec 6, 2019
@emonddr
emonddr approved these changes Dec 6, 2019
Copy link
Contributor

emonddr left a comment

Very cool

@bajtos bajtos merged commit 4d844d1 into master Dec 9, 2019
5 checks passed
5 checks passed
Travis CI - Pull Request Build Passed
Details
clahub All contributors have signed the Contributor License Agreement.
Details
continuous-integration/appveyor/branch AppVeyor build succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
coverage/coveralls Coverage increased (+0.005%) to 92.233%
Details
@bajtos bajtos deleted the feat/define-model-class branch Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can鈥檛 perform that action at this time.