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

feat(types): add InferAttributes utility type #13909

Merged
merged 29 commits into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f8dfa3b
feat(types): add `AttributesOf` utility type
ephys Jan 5, 2022
0351311
docs: document & typecheck `AttributesOf`
ephys Jan 5, 2022
74a3248
Merge branch 'main' into feature/attributesof
ephys Jan 5, 2022
f895c87
docs: make comments more explicit
ephys Jan 5, 2022
271e392
docs: hide maintainer-only information in typescript.md
ephys Jan 5, 2022
3f833fc
fix(types): revert accidental removal of Fn import
ephys Jan 5, 2022
75f17c4
feat(types): add `CreationAttributesOf`
ephys Jan 7, 2022
a55c39f
fix(types): make `undefined` fields optional in CreationAttributes
ephys Jan 7, 2022
932e618
feat(types): merge (Creation)AttributesOf, drop branded type
ephys Jan 7, 2022
11bcaf6
feat(types): bring back CreationOptional now that I understand branding
ephys Jan 7, 2022
7896f96
feat(types): add `NonAttribute` branded type
ephys Jan 7, 2022
60589bc
docs(types): document new attribute declaration system
ephys Jan 7, 2022
b4adc2a
fix(types): fix branded array support
ephys Jan 7, 2022
0922b2f
Merge branch 'main' into feature/attributesof
ephys Jan 7, 2022
a3bbc84
fix(types): fix array brand typing again
ephys Jan 7, 2022
7be44b2
Merge branch 'main' into feature/attributesof
ephys Jan 7, 2022
8d92e03
Merge branch 'main' into feature/attributesof
ephys Jan 8, 2022
17b7699
feat(types): add Attributes/CreationAttributes, rename AttributesOf
ephys Jan 8, 2022
1da9ce3
docs(typescript): add missing methods / remove readonly
ephys Jan 8, 2022
120e244
Merge branch 'main' into feature/attributesof
ephys Jan 11, 2022
2ffe137
Merge branch 'main' into feature/attributesof
ephys Jan 16, 2022
9e66864
Merge branch 'main' into feature/attributesof
ephys Jan 16, 2022
644afbe
refactor: add unused catch binding to workaround esbuild issue
ephys Jan 16, 2022
7c908e3
refactor: add unused catch binding to workaround esbuild issue
ephys Jan 16, 2022
7b97c0d
refactor: make esdocs able to compile typescript.md
ephys Jan 16, 2022
853926c
refactor: implement review changes
ephys Jan 17, 2022
21b8533
Merge branch 'main' into feature/attributesof
ephys Jan 21, 2022
e518b01
docs: specify InferAttributes release version + fix type names
ephys Jan 21, 2022
2706b51
docs: document TypeScript utility types
ephys Jan 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
263 changes: 160 additions & 103 deletions docs/manual/other-topics/typescript.md

Large diffs are not rendered by default.

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

Expand Down Expand Up @@ -303,7 +303,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
11 changes: 6 additions & 5 deletions types/lib/associations/has-many.d.ts
@@ -1,14 +1,15 @@
import { DataType } from '../data-types';
import {
CreateOptions,
CreateOptions, CreationAttributes,
ephys marked this conversation as resolved.
Show resolved Hide resolved
Filterable,
FindOptions,
InstanceUpdateOptions,
Model,
ModelCtor,
Transactionable
Transactionable,
} from '../model';
import { Association, ManyToManyOptions, MultiAssociationAccessors } from './base';
import { MakeUndefinedOptional } from '../utils';

/**
* Options provided when associating models with hasMany relationship
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>;
22 changes: 14 additions & 8 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,11 +97,17 @@ 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.
ephys marked this conversation as resolved.
Show resolved Hide resolved
* Use Attributes<Model> instead of this property to be forward-compatible.
*/
_attributes: TModelAttributes;
/**
* 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;

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