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

A weird TypeScript error when lazy loading a related entity #1082

Open
irshadahmad21 opened this issue Dec 23, 2024 · 4 comments
Open

A weird TypeScript error when lazy loading a related entity #1082

irshadahmad21 opened this issue Dec 23, 2024 · 4 comments

Comments

@irshadahmad21
Copy link

Package version

21.5.1

Describe the bug

I have relations with the simplified version being this

import { BaseModel, belongsTo, column } from '@adonisjs/lucid/orm';
import type { BelongsTo } from '@adonisjs/lucid/types/relations';

class Client extends BaseModel {
	@column({ isPrimary: true })
	declare id: number;
}

class Project extends BaseModel {
	@column({ isPrimary: true })
	declare id: number;

	@column()
	declare name: string;

	@column()
	declare clientId: string;

	@belongsTo(() => Client)
	declare client: BelongsTo<typeof Client>;
}

class Task extends BaseModel {
	@column({ isPrimary: true })
	declare id: number;

	@column()
	declare projectId: string;

	@belongsTo(() => Project)
	declare project: BelongsTo<typeof Project>;

	async getProjectName(this: Task) {
		await this.load('project');
		return this.project.name;
	}
}

I see an error on that this.load('project') call.

Screenshot 2024-12-23 at 9 57 58 PM
No overload matches this call.
  Overload 1 of 2, '(callback: (preloader: PreloaderContract<Task>) => void): Promise<void>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type '(preloader: PreloaderContract<Task>) => void'.
  Overload 2 of 2, '(relation: undefined, callback?: ((builder: never) => void) | undefined): Promise<void>', gave the following error.
    Argument of type '"project"' is not assignable to parameter of type 'undefined'.

It used to work fine in version 20.4.0. May be something changed in version 21.

Reproduction repo

No response

@irshadahmad21 irshadahmad21 changed the title A weird TypeScript error when using this.log('relation') A weird TypeScript error when using this.load('relation') Dec 23, 2024
@irshadahmad21 irshadahmad21 changed the title A weird TypeScript error when using this.load('relation') A weird TypeScript error when lazy loading a related entity Dec 23, 2024
@RomainLanz
Copy link
Member

Hey there! 👋🏻

This reminds me of an older TypeScript bug: microsoft/TypeScript#37778.

I'm not certain they're related, but does the issue persist if you downgrade Lucid or TypeScript? Also, could you create a minimal repository demonstrating the problem?

@thetutlage
Copy link
Member

This reminds me of an older TypeScript bug: microsoft/TypeScript#37778.

Its the same issue, since it was never fixed in the first place by the TypeScript team

@irshadahmad21
Copy link
Author

It may not be related to that bug because it works fine in Lucid v20 but breaks in v21.

Here are the minimal reproducible setups

With v20 (Build succeeds)
https://stackblitz.com/edit/vitejs-vite-8houbyjt?file=package.json

With v21 (Build fails)
https://stackblitz.com/edit/vitejs-vite-xcvgsb1y?file=package.json

@irshadahmad21
Copy link
Author

I think the bug was introduced in 532b9cb.

If you change

extends ModelRelations<LucidModel, LucidModel>

to

extends ModelRelations<infer _, infer __>

in

type ExtractModelRelations<Model extends LucidRow> = {
    [Key in keyof Model]: Model[Key] extends ModelRelations<LucidModel, LucidModel> ? Key : never;
}[keyof Model];

That may fix the issue. I don't know if that can have any other implications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants