Skip to content

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

Open
@irshadahmad21

Description

@irshadahmad21

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions