nestjs-knex
nestjs-knex copied to clipboard
Typescript 5 support
Hi, with typescript 5 there is an error
import { Injectable } from '@nestjs/common';
import { InjectKnex, Knex } from 'nestjs-knex';
@Injectable()
export class UsersService {
constructor(@InjectKnex() private readonly knex: Knex) {}
}
Unable to resolve signature of parameter decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'.ts(1239)
(alias) InjectKnex(connection?: string | undefined): (target: object, key: string | symbol, index?: number | undefined) => void
import InjectKnex
The current InjectKnex decorator in the published package is
export declare const InjectKnex: (connection?: string) => (target: object, key: string | symbol, index?: number) => void;
which results in an error when using InjectKnex() in a constructor since key is undefined there.
This problem has been addressed in nestjs and fixed (e.g. here) by adding an undefined
to key.
So the easiest way to fix this issue in this repo ist to just require NestJS v10 as a dev dependency. I just committed a PR for that.
In the meantime to let your code compile you could replace the { InjectKnex } import from nestjs-knex with your own:
export declare const InjectKnex: (connection?: string) => (target: object, key: string | symbol | undefined, index?: number) => void;
Any assistant how to actually use this? I also need to import InjectKnex
from this package and then have this declaration line as well right? I cannot seem to figure this out how to do this for some reason. Keep running on different TypeScript errors that doesn't seem to help with this.
I also tried to only import Knex
from this package and then have the declare line only. Like this:
import { Knex } from 'nestjs-knex';
export declare const InjectKnex: (connection?: string) => (target: object, key: string | symbol | undefined, index?: number) => void;
@Controller()
export class AppController {
constructor(
private readonly authService: AuthService,
@InjectKnex() private readonly knex: Knex,
) {}
}
But this results in an error like this:
random-project/src/app.controller.ts:10
@InjectKnex() private readonly knex: Knex,
^
TypeError: (0 , exports.InjectKnex) is not a function
A simple workaround that fixed the issue for me was the following:
import { InjectKnex as InjectKnexBroken, Knex } from "nestjs-knex";
const InjectKnex = InjectKnexBroken as (connection?: string) => (target: object, key: string | symbol | undefined, index?: number) => void;
Not a pretty solution, but it works.