'getCustomRepository returns a repository object with `this` being undefined when entities and repositories come from an npm package

I've moved my TypeORM entities, migrations and repositories to a shared npm package.

When using this package I'm trying to do const repo = getCustomRepository(MyRepo)

However, it appears that the repo has this as undefined. And I'm getting errors like TypeError: Cannot read properties of undefined (reading 'find').

Basically the same issue as in this Reddit thread: https://www.reddit.com/r/typescript/comments/p8932l/this_is_undefined_on_typeorm_custom_repository/

I'm not using NestJS in my case.

Does someone have a clue about what's going on and how to make it work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source