Skip to content

Commit 0f7e1d4

Browse files
Merge pull request #206 from RobinBuschmann/issue-204-scope-typings
closes #204 scope typings
2 parents a8cac9d + 52fbf46 commit 0f7e1d4

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

lib/models/Model.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {ICountOptions} from '../interfaces/ICountOptions';
2121
/* tslint:disable:array-type */
2222
/* tslint:disable:max-line-length */
2323
/* tslint:disable:max-classes-per-file */
24+
type ForTypeOf<T> = {[P in keyof T]: T[P]};
25+
type NonAbstractTypeOfModel<T> = (new () => T) & ForTypeOf<typeof Model>;
2426

2527
export declare abstract class Model<T> extends Hooks {
2628

@@ -132,7 +134,7 @@ export declare abstract class Model<T> extends Hooks {
132134
* @return Model A reference to the model, with the scope(s) applied. Calling scope again on the returned
133135
* model will clear the previous scope.
134136
*/
135-
static scope(options?: string | string[] | ScopeOptions | WhereOptions<any>): typeof Model;
137+
static scope<T>(this: NonAbstractTypeOfModel<T>, options?: string | string[] | ScopeOptions | WhereOptions<any>): NonAbstractTypeOfModel<T>;
136138

137139
/**
138140
* Search for multiple instances.
@@ -407,7 +409,7 @@ export declare abstract class Model<T> extends Hooks {
407409
/**
408410
* Unscope the model
409411
*/
410-
static unscoped(): typeof Model;
412+
static unscoped<T>(this: NonAbstractTypeOfModel<T>): NonAbstractTypeOfModel<T>;
411413

412414
/**
413415
* A reference to the sequelize instance

test/specs/scopes.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('scopes', () => {
7070

7171
it('should consider other scopes', () =>
7272

73-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes).findOne()
73+
ShoeWithScopes.scope('full').findOne()
7474
.then(shoe => {
7575

7676
expect(shoe).to.have.property('manufacturer').which.is.not.null;
@@ -89,8 +89,8 @@ describe('scopes', () => {
8989
);
9090

9191
it('should not consider default scope due to unscoped call', () =>
92-
(ShoeWithScopes
93-
.unscoped() as typeof ShoeWithScopes)
92+
ShoeWithScopes
93+
.unscoped()
9494
.findOne()
9595
.then(shoe => {
9696
expect(shoe).to.have.property('secretKey').which.is.a('string');
@@ -101,7 +101,7 @@ describe('scopes', () => {
101101

102102
it('should consider scopes and additional included model (object)', () =>
103103
expect(
104-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
104+
ShoeWithScopes.scope('full')
105105
.findOne({
106106
include: [{
107107
model: Person,
@@ -117,7 +117,7 @@ describe('scopes', () => {
117117

118118
it('should consider scopes and additional included model (model)', () =>
119119
expect(
120-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
120+
ShoeWithScopes.scope('full')
121121
.findOne({
122122
include: [Person]
123123
})
@@ -131,7 +131,7 @@ describe('scopes', () => {
131131

132132
it('should not consider default scope due to unscoped call, but additonal includes (object)', () =>
133133

134-
(ShoeWithScopes.unscoped() as typeof ShoeWithScopes)
134+
ShoeWithScopes.unscoped()
135135
.findOne({
136136
include: [{model: Person}]
137137
})
@@ -143,8 +143,8 @@ describe('scopes', () => {
143143

144144
it('should not consider default scope due to unscoped call, but additonal includes (model)', () =>
145145

146-
(ShoeWithScopes
147-
.unscoped() as typeof ShoeWithScopes)
146+
ShoeWithScopes
147+
.unscoped()
148148
.findOne({
149149
include: [Person]
150150
})
@@ -169,7 +169,7 @@ describe('scopes', () => {
169169
);
170170

171171
it('should consider scope of included model (with own scope)', () =>
172-
(ShoeWithScopes.scope('red') as typeof ShoeWithScopes)
172+
ShoeWithScopes.scope('red')
173173
.findOne({
174174
include: [Manufacturer.scope('brandOnly') as typeof Manufacturer]
175175
})
@@ -187,7 +187,7 @@ describe('scopes', () => {
187187
describe('with nested scope', () => {
188188

189189
it('should consider nested scope', () =>
190-
(ShoeWithScopes.scope('manufacturerWithScope') as typeof ShoeWithScopes)
190+
ShoeWithScopes.scope('manufacturerWithScope')
191191
.findOne()
192192
.then(shoe => {
193193
expect(shoe).to.have.property('manufacturer')
@@ -197,7 +197,7 @@ describe('scopes', () => {
197197
);
198198

199199
it('should not consider nested scope', () =>
200-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
200+
ShoeWithScopes.scope('full')
201201
.findOne()
202202
.then(shoe => {
203203
expect(shoe).to.have.property('manufacturer')
@@ -211,15 +211,15 @@ describe('scopes', () => {
211211
describe('with scope function', () => {
212212

213213
it('should find appropriate shoe due to correctly passed scope function param', () =>
214-
(ShoeWithScopes.scope({method: ['primaryColor', 'red']}) as typeof ShoeWithScopes)
214+
ShoeWithScopes.scope({method: ['primaryColor', 'red']})
215215
.findOne()
216216
.then(shoe => {
217217
expect(shoe).to.have.property('primaryColor', 'red');
218218
})
219219
);
220220

221221
it('should find appropriate shoe due to correctly passed scope function param including associated model', () =>
222-
(ShoeWithScopes.scope({method: ['primaryColorWithManufacturer', 'red']}) as typeof ShoeWithScopes)
222+
ShoeWithScopes.scope({method: ['primaryColorWithManufacturer', 'red']})
223223
.findOne()
224224
.then(shoe => {
225225
expect(shoe).to.have.property('primaryColor', 'red');

0 commit comments

Comments
 (0)