lambrohan / nestjs-meilisearch

Meilisearch module for nestjs applications.
https://www.npmjs.com/package/nestjs-meilisearch
MIT License
22 stars 7 forks source link

MeiliSearch @InjectMeilisearch Decorator wrong types #10

Closed Gradlon closed 7 months ago

Gradlon commented 9 months ago

When using the Inject Decorator I get the Following error:

TS1239: 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'.

The Code look like this.

import { Injectable } from '@nestjs/common';
import { MeiliSearch, ResourceResults } from 'meilisearch';
import { InjectMeiliSearch } from 'nestjs-meilisearch';
@Injectable()
export class MeiliSearchService {
    public constructor(
        @InjectMeiliSearch() private readonly meiliSearch: MeiliSearch,
    ) {}
    public async db(): Promise<ResourceResults<any>> {
        const index = await this.meiliSearch.getIndex('person-index');
        console.log(index);

        return await index.getDocuments();
    }
}

An easy fix would be to change

export declare function InjectMeiliSearch(): (target: object, key: string | symbol, index?: number) => void;

to

export declare function InjectMeiliSearch(): (target: object, key?: string | symbol, index?: number) => void;

Would this be viable?

lambrohan commented 9 months ago

@Gradlon I'm unable to reproduce this with

    "meilisearch": "^0.37.0",
    "nestjs-meilisearch": "^2.1.5",
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",

    nodejs 20.10.0

Can you share a example which reproduces this?

Synatiic commented 9 months ago

Can confirm the issue.

nodejs v20.11.0
"meilisearch": "^0.37.0",
"nestjs-meilisearch": "^2.1.5",
"@nestjs/common": "^10.2.7",
"@nestjs/core": "^10.2.7",
"typescript": "^5.2.2",

@Gradlon's suggested fix works just fine

lambrohan commented 9 months ago

type of InjectMeilisearch is inferred from Inject, which is provided by @nestjs/common

import { Inject } from '@nestjs/common';
import { MEILI_CLIENT } from '../constants';

export function InjectMeiliSearch() {
  return Inject(MEILI_CLIENT);
}

Here is a demo project running the meilisearch module, can you please help me reproduce here? https://stackblitz.com/edit/nestjs-typescript-starter-uvzcw9?file=src%2Fapp.service.ts

PureSci commented 8 months ago

I also face the same issue but can't seem to reproduce

vince8x commented 7 months ago

I have same issue: Node: 20.2.0 "@nestjs/common": "^10.3.3" "@nestjs/core":"10.3.3" "meilisearch": "^0.38.0" "nestjs-meilisearch": 2.1.5

lambrohan commented 7 months ago

typescript version? @vince8x

nktpr commented 7 months ago

I used your demo project (https://stackblitz.com/edit/nestjs-typescript-starter-uvzcw9?file=src%2Fapp.service.ts). When I add "strict": true to the tsconfig.json file, the above error appears @lambrohan

lambrohan commented 7 months ago

it was issue with the older nestjs packages. Since InjectMeilisearch was only inferring types as per Inject from nestjs-common package. Fixed this issue in new release https://github.com/lambrohan/nestjs-meilisearch/releases/tag/v2.1.6