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 all 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
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.
ephys marked this conversation as resolved.
Show resolved Hide resolved
* 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this private? (same with the other places where this comment is)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't yet, it would break user code, but I can in v7

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add that to the TODO as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think of it, if it's private, Attribute<M> will not be able to access it. It's going to have to be a public internal Symbol I think

/**
* 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