Skip to content

Commit 1c60b4b

Browse files
Merge pull request #249 from Danlock/master
narrow down types for attributes passed to functions like Model.create/Model.build etc
2 parents 37f6cac + 92a9ed7 commit 1c60b4b

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);
@@ -294,29 +306,29 @@ export declare abstract class Model<T> extends Hooks {
294306
/**
295307
* Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty.
296308
*/
297-
static build<T extends Model<T>>(this: (new () => T), record?: any, options?: IBuildOptions): T;
309+
static build<T extends Model<T>>(this: (new () => T), record?: GetAttributes<T>, options?: IBuildOptions): T;
298310
static build<T extends Model<T>, A>(this: (new () => T), record?: A, options?: IBuildOptions): T;
299311

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

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

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

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

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

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

343355
/**
@@ -374,7 +386,7 @@ export declare abstract class Model<T> extends Hooks {
374386
*
375387
* @param records List of objects (key/value pairs) to create instances from
376388
*/
377-
static bulkCreate<T extends Model<T>>(this: (new () => T), records: any[], options?: BulkCreateOptions): Promise<T[]>;
389+
static bulkCreate<T extends Model<T>>(this: (new () => T), records: GetAttributes<T>[], options?: BulkCreateOptions): Promise<T[]>;
378390
static bulkCreate<T extends Model<T>, A>(this: (new () => T), records: A[], options?: BulkCreateOptions): Promise<T[]>;
379391

380392
/**
@@ -399,7 +411,7 @@ export declare abstract class Model<T> extends Hooks {
399411
* elements. The first element is always the number of affected rows, while the second element is the actual
400412
* affected rows (only supported in postgres with `options.returning` true.)
401413
*/
402-
static update<T extends Model<T>>(this: (new () => T), values: any, options: UpdateOptions): Promise<[number, Array<T>]>;
414+
static update<T extends Model<T>>(this: (new () => T), values: GetAttributes<T>, options: UpdateOptions): Promise<[number, Array<T>]>;
403415
static update<T extends Model<T>, A>(this: (new () => T), values: A, options: UpdateOptions): Promise<[number, Array<T>]>;
404416
/**
405417
* 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)