daniel7grant / eslint-plugin-typeorm-typescript

An ESLint plugin to catch mistakes if the TypeORM column types and TypeScript types aren't consistent.
MIT License
6 stars 1 forks source link

typeorm-typescript/enforce-relation-types, support "Relation<Other>" #17

Open norbornen opened 5 days ago

norbornen commented 5 days ago

Hello!

Can u add support for this case:

import { Column, Entity, OneToOne, OneToMany, Relation } from 'typeorm';

class Entity {
    // Should be Other | null
    @OneToOne(() => Other)
    @JoinColumn()
    other: Relation<Other>;

    @OneToMany(() => Other, ...)
    other2: Relation<Other>[];
}

https://github.com/typeorm/typeorm/issues/4526#issuecomment-1076462998

This is the correct description of the column, but the linter reports an error:

Type of other in Entity is not consistent with the TypeORM relation type OneToMany
(expected type: Other).eslint[typeorm-typescript/enforce-relation-types](https://github.com/daniel7grant/eslint-plugin-typeorm-typescript#typeorm-typescriptenforce-relation-types)

Thank you for your work!

daniel7grant commented 4 days ago

Thank you for the issue, there is already an ongoing PR in the form of #16 about adding a relation wrapper. I didn't have time so far to look over the implementation but I will try to do it in the following days.

daniel7grant commented 4 days ago

It turned out to be a smaller change than expected, I added this in #18. Feel free to test it and I'll put out a new patch version in the following days.

norbornen commented 3 days ago

Thank for your answer!

pr18 good work for OneToOne relation, but I'm a little confused by the fact that he expects type Relation<Other | null>, while it seems more appropriate to use type Relation<Other> | null (and Relation<Other>[] | null for Many).

pr18 not work for OneToMany relation: Снимок экрана 2024-11-04 в 16 30 13

norbornen commented 3 days ago

And, just so you know, the plugin does not work correctly if "strictNullChecks": false in project. I am sure that this is the problem of those who use it, but I think it would be right to write this to README.

Снимок экрана 2024-11-04 в 16 50 15

daniel7grant commented 3 days ago

Thanks for testing!

As for the order of the wrapper, I've chosen somewhat arbitrarily Relation<T[]> , mostly based on this Stack Overflow thread. I couldn't find anything in the TypeORM docs, so I just used this answer. If you can point me towards any official suggestion, I'll change it.

I tried with OneToMany, it seems to be working to me. Your problem might be that if you misspell the name, it won't be added to the suggestion either: d: Relation<Aaa> will suggest d: Relation<Aaa[]> correctly.

For strictNullChecks, I wasn't able to reproduce it. Can you send me a tsconfig or anything that makes it break with the example configuration? Assuming this is a real problem, I'm not sure how to deal with it. I'll probably follow your advice and add a note to the README... without strict null-checking, this is mostly a cosmetic library.