Skip to content

Commit 489497c

Browse files
authored
feat: fix types for legacy decorator syntax (#9250)
1 parent 161cbd2 commit 489497c

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

packages/model/src/-private/belongs-to.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ export function belongsTo(): never;
275275
export function belongsTo(type: string): never;
276276
export function belongsTo<T>(
277277
type: TypeFromInstance<NoNull<T>>,
278-
options: RelationshipOptions<T, false>
278+
options: RelationshipOptions<T, boolean>
279279
): RelationshipDecorator<T>;
280-
export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
281-
type: TypeFromInstance<NoNull<T>>,
282-
options: RelationshipOptions<T, true>
283-
): RelationshipDecorator<K>;
280+
// export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
281+
// type: TypeFromInstance<NoNull<T>>,
282+
// options: RelationshipOptions<T, true>
283+
// ): RelationshipDecorator<K>;
284284
export function belongsTo(type: string, options: RelationshipOptions<unknown, boolean>): RelationshipDecorator<unknown>;
285285
export function belongsTo<T>(
286286
type?: TypeFromInstance<NoNull<T>>,

packages/model/src/-private/has-many.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ export function hasMany(): never;
258258
export function hasMany(type: string): never;
259259
export function hasMany<T>(
260260
type: TypeFromInstance<NoNull<T>>,
261-
options: RelationshipOptions<T, false>
261+
options: RelationshipOptions<T, boolean>
262262
): RelationshipDecorator<T>;
263-
export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
264-
type: TypeFromInstance<NoNull<T>>,
265-
options: RelationshipOptions<T, true>
266-
): RelationshipDecorator<K>;
263+
// export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
264+
// type: TypeFromInstance<NoNull<T>>,
265+
// options: RelationshipOptions<T, true>
266+
// ): RelationshipDecorator<K>;
267267
export function hasMany(type: string, options: RelationshipOptions<unknown, boolean>): RelationshipDecorator<unknown>;
268268
export function hasMany<T>(
269269
type?: TypeFromInstance<NoNull<T>>,

packages/model/src/-private/model.type-test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ expectTypeOf(branded.bestFriend).toEqualTypeOf<BrandedUser>();
6060
expectTypeOf<Awaited<typeof branded.friends>>().toEqualTypeOf<ManyArray<BrandedUser>>();
6161
expectTypeOf<Awaited<typeof branded.twin>>().toEqualTypeOf<BrandedUser | null>();
6262

63+
class BrandedTypedUser extends Model {
64+
@attr('string') declare name: string | null;
65+
@hasMany<BrandedTypedUser>('user', { async: false, inverse: null }) declare enemies: ManyArray<BrandedTypedUser>;
66+
@belongsTo<BrandedTypedUser>('user', { async: false, inverse: null }) declare bestFriend: BrandedTypedUser;
67+
@hasMany<BrandedTypedUser>('user', { async: true, inverse: 'friends' })
68+
declare friends: PromiseManyArray<BrandedTypedUser>;
69+
@belongsTo<BrandedTypedUser>('user', { async: true, inverse: 'twin' })
70+
declare twin: PromiseBelongsTo<BrandedTypedUser>;
71+
@hasMany<BrandedTypedUser>('user', { async: false, inverse: 'leader' })
72+
declare crew: PromiseManyArray<BrandedTypedUser>;
73+
@belongsTo<BrandedTypedUser>('user', { async: false, inverse: 'crew' })
74+
declare leader: PromiseBelongsTo<BrandedTypedUser>;
75+
76+
[ResourceType] = 'user' as const;
77+
}
78+
const brandedAndTyped = new BrandedTypedUser();
79+
80+
expectTypeOf<Awaited<PromiseManyArray<BrandedTypedUser>>['modelName']>().toEqualTypeOf<'user'>();
81+
expectTypeOf<ManyArray<BrandedTypedUser>['modelName']>().toEqualTypeOf<'user'>();
82+
expectTypeOf<ManyArray<BrandedTypedUser>>().toMatchTypeOf<BrandedTypedUser[]>();
83+
84+
expectTypeOf(brandedAndTyped.name).toEqualTypeOf<string | null>();
85+
expectTypeOf(brandedAndTyped.enemies).toEqualTypeOf<ManyArray<BrandedTypedUser>>();
86+
expectTypeOf(brandedAndTyped.bestFriend).toEqualTypeOf<BrandedTypedUser>();
87+
expectTypeOf<Awaited<typeof brandedAndTyped.friends>>().toEqualTypeOf<ManyArray<BrandedTypedUser>>();
88+
expectTypeOf<Awaited<typeof brandedAndTyped.twin>>().toEqualTypeOf<BrandedTypedUser | null>();
89+
6390
// ------------------------------
6491
// References
6592
// ------------------------------

0 commit comments

Comments
 (0)