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(repository): fix DynamicModelCtor type to correctly preserve Props #5600

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('defineModelClass', () => {
expect(instance.title).to.equal('a title');
});

it('creates a correctly typed constructor', () => {
const definition = new ModelDefinition('Product').addProperty('name', {
type: 'string',
});
const Product = defineModelClass<typeof Model, {name: string}>(
Model,
definition,
);

// When the `data` argument is not provided, then TypeScript may pick
// Model's constructor signature instead of Product constructor signature.
// When that happens, `p` has type `Model` without Product properties.
// This test verifies that such situation does not happen.
const p = new Product();
p.name = 'Pen';
// The test passed when the line above is accepted by the compiler.
});

it('creates an Entity class', () => {
const definition = new ModelDefinition('Product')
.addProperty('id', {type: 'number', id: true})
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/define-model-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export function defineModelClass<
export type DynamicModelCtor<
BaseCtor extends typeof Model,
Props extends object
> = BaseCtor & {
> = {
/** Model constructor accepting partial model data. */
new (data?: DataObject<PrototypeOf<BaseCtor> & Props>): PrototypeOf<
BaseCtor
> &
Props;
};
} & BaseCtor;