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

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

Closed
norbornen opened this issue Nov 2, 2024 · 6 comments
Closed

Comments

@norbornen
Copy link

norbornen commented Nov 2, 2024

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>[];
}

typeorm/typeorm#4526 (comment)

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

norbornen commented Nov 4, 2024

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
Copy link
Owner

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.

@daniel7grant
Copy link
Owner

I merged the wrapper configuration in #18 with this setup. If you have any more issues feel free to reopen.

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

2 participants