Skip to content

Commit

Permalink
feat(types): add InferAttributes utility type (#13909)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
2 people authored and sdepold committed Jan 22, 2022
1 parent dd49044 commit fd42687
Show file tree
Hide file tree
Showing 19 changed files with 953 additions and 418 deletions.
357 changes: 254 additions & 103 deletions docs/manual/other-topics/typescript.md

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions types/lib/associations/belongs-to-many.d.ts
@@ -1,6 +1,7 @@
import {
BulkCreateOptions,
CreateOptions,
CreationAttributes,
Filterable,
FindAttributeOptions,
FindOptions,
Expand All @@ -9,7 +10,7 @@ import {
Model,
ModelCtor,
ModelType,
Transactionable
Transactionable,
} from '../model';
import { Association, AssociationScope, ForeignKeyOptions, ManyToManyOptions, MultiAssociationAccessors } from './base';

Expand Down Expand Up @@ -303,7 +304,7 @@ export interface BelongsToManyCreateAssociationMixinOptions extends CreateOption
* @see Instance
*/
export type BelongsToManyCreateAssociationMixin<TModel extends Model> = (
values?: TModel['_creationAttributes'],
values?: CreationAttributes<TModel>,
options?: BelongsToManyCreateAssociationMixinOptions
) => Promise<TModel>;

Expand Down
4 changes: 2 additions & 2 deletions types/lib/associations/belongs-to.d.ts
@@ -1,5 +1,5 @@
import { DataType } from '../data-types';
import { CreateOptions, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
import { CreateOptions, CreationAttributes, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
import { Association, AssociationOptions, SingleAssociationAccessors } from './base';

// type ModelCtor<M extends Model> = InstanceType<typeof M>;
Expand Down Expand Up @@ -117,7 +117,7 @@ export interface BelongsToCreateAssociationMixinOptions
* @see Instance
*/
export type BelongsToCreateAssociationMixin<TModel extends Model> = (
values?: TModel['_creationAttributes'],
values?: CreationAttributes<TModel>,
options?: BelongsToCreateAssociationMixinOptions
) => Promise<TModel>;

Expand Down
9 changes: 5 additions & 4 deletions types/lib/associations/has-many.d.ts
@@ -1,12 +1,13 @@
import { DataType } from '../data-types';
import {
CreateOptions,
CreationAttributes,
Filterable,
FindOptions,
InstanceUpdateOptions,
Model,
ModelCtor,
Transactionable
Transactionable,
} from '../model';
import { Association, ManyToManyOptions, MultiAssociationAccessors } from './base';

Expand Down Expand Up @@ -211,10 +212,10 @@ export interface HasManyCreateAssociationMixinOptions extends CreateOptions<any>
*/
export type HasManyCreateAssociationMixin<
TModel extends Model,
TForeignKey extends keyof TModel['_creationAttributes'] = never,
TScope extends keyof TModel['_creationAttributes'] = never
TForeignKey extends keyof CreationAttributes<TModel> = never,
TScope extends keyof CreationAttributes<TModel> = never
> = (
values?: Omit<TModel['_creationAttributes'], TForeignKey | TScope>,
values?: Omit<CreationAttributes<TModel>, TForeignKey | TScope>,
options?: HasManyCreateAssociationMixinOptions
) => Promise<TModel>;

Expand Down
4 changes: 2 additions & 2 deletions types/lib/associations/has-one.d.ts
@@ -1,5 +1,5 @@
import { DataType } from '../data-types';
import { CreateOptions, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
import { CreateOptions, CreationAttributes, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
import { Association, AssociationOptions, SingleAssociationAccessors } from './base';

/**
Expand Down Expand Up @@ -114,6 +114,6 @@ export interface HasOneCreateAssociationMixinOptions extends HasOneSetAssociatio
* @see Instance
*/
export type HasOneCreateAssociationMixin<TModel extends Model> = (
values?: TModel['_creationAttributes'],
values?: CreationAttributes<TModel>,
options?: HasOneCreateAssociationMixinOptions
) => Promise<TModel>;
26 changes: 16 additions & 10 deletions types/lib/hooks.d.ts
@@ -1,4 +1,4 @@
import { ModelType } from '../index';
import { Attributes, CreationAttributes, ModelType } from '../index';
import { ValidationOptions } from './instance-validator';
import Model, {
BulkCreateOptions,
Expand Down Expand Up @@ -97,13 +97,19 @@ export class Hooks<
/**
* A similar dummy variable that doesn't exist on the real object. Do not
* try to access this in real code.
*
* @deprecated This property will become a Symbol in v7 to prevent collisions.
* Use Attributes<Model> instead of this property to be forward-compatible.
*/
_attributes: TModelAttributes;
_attributes: TModelAttributes; // TODO [>6]: make this a non-exported symbol (same as the one in model.d.ts)
/**
* A similar dummy variable that doesn't exist on the real object. Do not
* try to access this in real code.
*
* @deprecated This property will become a Symbol in v7 to prevent collisions.
* Use CreationAttributes<Model> instead of this property to be forward-compatible.
*/
_creationAttributes: TCreationAttributes;
_creationAttributes: TCreationAttributes; // TODO [>6]: make this a non-exported symbol (same as the one in model.d.ts)

/**
* Add a hook to the model
Expand All @@ -113,28 +119,28 @@ export class Hooks<
*/
public static addHook<
H extends Hooks,
K extends keyof SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>
K extends keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>
>(
this: HooksStatic<H>,
hookType: K,
name: string,
fn: SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>[K]
fn: SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>[K]
): HooksCtor<H>;
public static addHook<
H extends Hooks,
K extends keyof SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>
K extends keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>
>(
this: HooksStatic<H>,
hookType: K,
fn: SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>[K]
fn: SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>[K]
): HooksCtor<H>;

/**
* Remove hook from the model
*/
public static removeHook<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
name: string,
): HooksCtor<H>;

Expand All @@ -143,11 +149,11 @@ export class Hooks<
*/
public static hasHook<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
): boolean;
public static hasHooks<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], H['_attributes'], H['_creationAttributes']>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
): boolean;

/**
Expand Down

0 comments on commit fd42687

Please sign in to comment.