Skip to content

Commit 92a9ed7

Browse files
author
Danlock
authored
narrow down types for attributes passed to functions like Model.create/Model.build etc
1 parent 634a9ff commit 92a9ed7

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

lib/models/Model.d.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import {ICountOptions} from '../interfaces/ICountOptions';
2424
type ForTypeOf<T> = {[P in keyof T]: T[P]};
2525
type NonAbstractTypeOfModel<T> = (new () => T) & ForTypeOf<typeof Model>;
2626

27+
type Diff<T extends string, U extends string> = ({[P in T]: P} & {[P in U]: never} & {[x: string]: never})[T];
28+
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
29+
30+
type GetAttributes<T extends Model<T>> =
31+
Partial<Omit<T,keyof Model<any>>> | {
32+
id?: number|any;
33+
createdAt?: Date|any;
34+
updatedAt?: Date|any;
35+
deletedAt?: Date|any;
36+
version?: number|any;
37+
};
38+
2739
export declare abstract class Model<T> extends Hooks {
2840

2941
constructor(values?: any, options?: IBuildOptions);
@@ -293,29 +305,29 @@ export declare abstract class Model<T> extends Hooks {
293305
/**
294306
* Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty.
295307
*/
296-
static build<T extends Model<T>>(this: (new () => T), record?: any, options?: IBuildOptions): T;
308+
static build<T extends Model<T>>(this: (new () => T), record?: GetAttributes<T>, options?: IBuildOptions): T;
297309
static build<T extends Model<T>, A>(this: (new () => T), record?: A, options?: IBuildOptions): T;
298310

299311
/**
300312
* Undocumented bulkBuild
301313
*/
302-
static bulkBuild<T extends Model<T>>(this: (new () => T), records: any[], options?: IBuildOptions): T[];
314+
static bulkBuild<T extends Model<T>>(this: (new () => T), records: GetAttributes<T>[], options?: IBuildOptions): T[];
303315
static bulkBuild<T extends Model<T>, A>(this: (new () => T), records: A[], options?: IBuildOptions): T[];
304316

305317
/**
306318
* Builds a new model instance and calls save on it.
307319
*/
308-
static create<T extends Model<T>>(this: (new () => T), values?: any, options?: ICreateOptions): Promise<T>;
320+
static create<T extends Model<T>>(this: (new () => T), values?: GetAttributes<T>, options?: ICreateOptions): Promise<T>;
309321
static create<T extends Model<T>, A>(this: (new () => T), values?: A, options?: ICreateOptions): Promise<T>;
310322

311323
/**
312324
* Find a row that matches the query, or build (but don't save) the row if none is found.
313325
* The successfull result of the promise will be (instance, initialized) - Make sure to use .spread()
314326
*/
315-
static findOrInitialize<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
327+
static findOrInitialize<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<GetAttributes<T>>): Promise<[T, boolean]>;
316328
static findOrInitialize<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
317329

318-
static findOrBuild<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
330+
static findOrBuild<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<GetAttributes<T>>): Promise<[T, boolean]>;
319331
static findOrBuild<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
320332

321333
/**
@@ -329,14 +341,14 @@ export declare abstract class Model<T> extends Hooks {
329341
* an instance of sequelize.TimeoutError will be thrown instead. If a transaction is created, a savepoint
330342
* will be created instead, and any unique constraint violation will be handled internally.
331343
*/
332-
static findOrCreate<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
344+
static findOrCreate<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<GetAttributes<T>>): Promise<[T, boolean]>;
333345
static findOrCreate<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
334346

335347
/**
336348
* A more performant findOrCreate that will not work under a transaction (at least not in postgres)
337349
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
338350
*/
339-
static findCreateFind<T extends Model<T>>(this: (new () => T), options: IFindCreateFindOptions<any>): Promise<[T, boolean]>;
351+
static findCreateFind<T extends Model<T>>(this: (new () => T), options: IFindCreateFindOptions<GetAttributes<T>>): Promise<[T, boolean]>;
340352
static findCreateFind<T extends Model<T>, A>(this: (new () => T), options: IFindCreateFindOptions<A>): Promise<[T, boolean]>;
341353

342354
/**
@@ -373,7 +385,7 @@ export declare abstract class Model<T> extends Hooks {
373385
*
374386
* @param records List of objects (key/value pairs) to create instances from
375387
*/
376-
static bulkCreate<T extends Model<T>>(this: (new () => T), records: any[], options?: BulkCreateOptions): Promise<T[]>;
388+
static bulkCreate<T extends Model<T>>(this: (new () => T), records: GetAttributes<T>[], options?: BulkCreateOptions): Promise<T[]>;
377389
static bulkCreate<T extends Model<T>, A>(this: (new () => T), records: A[], options?: BulkCreateOptions): Promise<T[]>;
378390

379391
/**
@@ -398,7 +410,7 @@ export declare abstract class Model<T> extends Hooks {
398410
* elements. The first element is always the number of affected rows, while the second element is the actual
399411
* affected rows (only supported in postgres with `options.returning` true.)
400412
*/
401-
static update<T extends Model<T>>(this: (new () => T), values: any, options: UpdateOptions): Promise<[number, Array<T>]>;
413+
static update<T extends Model<T>>(this: (new () => T), values: GetAttributes<T>, options: UpdateOptions): Promise<[number, Array<T>]>;
402414
static update<T extends Model<T>, A>(this: (new () => T), values: A, options: UpdateOptions): Promise<[number, Array<T>]>;
403415
/**
404416
* Run a describe query on the table. The result will be return to the listener as a hash of attributes and

0 commit comments

Comments
 (0)