不要怂,就是干,撸起袖子干!

Commit 287607a0 by papb

fix(types): better support for readonly arrays

1 parent 914279aa
...@@ -40,8 +40,8 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> { ...@@ -40,8 +40,8 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> {
instance: M, instance: M,
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes> options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>
): HookReturn; ): HookReturn;
beforeBulkCreate(instances: M[], options: BulkCreateOptions<TAttributes>): HookReturn; beforeBulkCreate(instances: readonly M[], options: BulkCreateOptions<TAttributes>): HookReturn;
afterBulkCreate(instances: M[], options: BulkCreateOptions<TAttributes>): HookReturn; afterBulkCreate(instances: readonly M[], options: BulkCreateOptions<TAttributes>): HookReturn;
beforeBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn; beforeBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn;
afterBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn; afterBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn;
beforeBulkRestore(options: RestoreOptions<TAttributes>): HookReturn; beforeBulkRestore(options: RestoreOptions<TAttributes>): HookReturn;
...@@ -52,7 +52,7 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> { ...@@ -52,7 +52,7 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> {
beforeCount(options: CountOptions<TAttributes>): HookReturn; beforeCount(options: CountOptions<TAttributes>): HookReturn;
beforeFindAfterExpandIncludeAll(options: FindOptions<TAttributes>): HookReturn; beforeFindAfterExpandIncludeAll(options: FindOptions<TAttributes>): HookReturn;
beforeFindAfterOptions(options: FindOptions<TAttributes>): HookReturn; beforeFindAfterOptions(options: FindOptions<TAttributes>): HookReturn;
afterFind(instancesOrInstance: M[] | M | null, options: FindOptions<TAttributes>): HookReturn; afterFind(instancesOrInstance: readonly M[] | M | null, options: FindOptions<TAttributes>): HookReturn;
beforeSync(options: SyncOptions): HookReturn; beforeSync(options: SyncOptions): HookReturn;
afterSync(options: SyncOptions): HookReturn; afterSync(options: SyncOptions): HookReturn;
beforeBulkSync(options: SyncOptions): HookReturn; beforeBulkSync(options: SyncOptions): HookReturn;
......
import {Model, SaveOptions, Sequelize, FindOptions} from "sequelize" import { expectTypeOf } from "expect-type";
import { Model, SaveOptions, Sequelize, FindOptions } from "sequelize";
import { ModelHooks } from "../lib/hooks"; import { ModelHooks } from "../lib/hooks";
/* class TestModel extends Model {}
* covers types/lib/sequelize.d.ts
*/
Sequelize.beforeSave((t: TestModel, options: SaveOptions) => {});
Sequelize.afterSave((t: TestModel, options: SaveOptions) => {});
Sequelize.afterFind((t: TestModel[] | TestModel | null, options: FindOptions) => {});
Sequelize.afterFind('namedAfterFind', (t: TestModel[] | TestModel | null, options: FindOptions) => {});
/* /*
* covers types/lib/hooks.d.ts * covers types/lib/hooks.d.ts
*/ */
export const sequelize = new Sequelize('uri', {
hooks: {
beforeSave (m: Model, options: SaveOptions) {},
afterSave (m: Model, options: SaveOptions) {},
afterFind (m: Model[] | Model | null, options: FindOptions) {},
}
});
class TestModel extends Model {
}
const hooks: Partial<ModelHooks> = { const hooks: Partial<ModelHooks> = {
beforeSave(t: TestModel, options: SaveOptions) { }, beforeSave(m, options) {
afterSave(t: TestModel, options: SaveOptions) { }, expectTypeOf(m).toEqualTypeOf<TestModel>();
afterFind(t: TestModel | TestModel[] | null, options: FindOptions) { }, expectTypeOf(options).toMatchTypeOf<SaveOptions>(); // TODO consider `.toEqualTypeOf` instead ?
},
afterSave(m, options) {
expectTypeOf(m).toEqualTypeOf<TestModel>();
expectTypeOf(options).toMatchTypeOf<SaveOptions>(); // TODO consider `.toEqualTypeOf` instead ?
},
afterFind(m, options) {
expectTypeOf(m).toEqualTypeOf<readonly TestModel[] | TestModel | null>();
expectTypeOf(options).toEqualTypeOf<FindOptions>();
}
}; };
TestModel.init({}, {sequelize, hooks }) export const sequelize = new Sequelize('uri', { hooks });
TestModel.init({}, { sequelize, hooks });
TestModel.addHook('beforeSave', (t: TestModel, options: SaveOptions) => { }); TestModel.addHook('beforeSave', hooks.beforeSave!);
TestModel.addHook('afterSave', (t: TestModel, options: SaveOptions) => { }); TestModel.addHook('afterSave', hooks.afterSave!);
TestModel.addHook('afterFind', (t: TestModel[] | TestModel | null, options: FindOptions) => { }); TestModel.addHook('afterFind', hooks.afterFind!);
/* /*
* covers types/lib/model.d.ts * covers types/lib/model.d.ts
*/ */
TestModel.beforeSave((t: TestModel, options: SaveOptions) => { }); TestModel.beforeSave(hooks.beforeSave!);
TestModel.afterSave((t: TestModel, options: SaveOptions) => { }); TestModel.afterSave(hooks.afterSave!);
TestModel.afterFind((t: TestModel | TestModel[] | null, options: FindOptions) => { }); TestModel.afterFind(hooks.afterFind!);
/*
* covers types/lib/sequelize.d.ts
*/
Sequelize.beforeSave(hooks.beforeSave!);
Sequelize.afterSave(hooks.afterSave!);
Sequelize.afterFind(hooks.afterFind!);
Sequelize.afterFind('namedAfterFind', hooks.afterFind!);
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!