ウェブサイト検索

typescript - NestJS: 型 'string' の引数は型 'never' のパラメータに割り当てられません


import { INestApplication, Injectable, OnModuleInit} from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
    async onModuleInit() {
        await this.$connect();
    }

    async enableShutdownHooks(app: INestApplication) {
        this.$on('beforeExit', async () => {
          await app.close();
        });
    }
} 

私は次の行でエラーが発生しました this.$on('beforeExit', async() => { というのも、Argument of type 'string' is not assignable to parameter of type 'never' と表示されたからです

私はStack Overflowからたくさんのことを試しましたが、解決できませんでした。どうか助けてください。

それはうまく動作するはずだった。

回答 #1

こんなふうにやってみてください、GitHubのページで見て、私にはうまくいきました

    async enableShutdownHooks(app: INestApplication) {
    process.on('beforeExit', () => {
      app.close();
    });
  }

回答 #2

これは Prisma V5 では非推奨です

async enableShutdownHooks(app: INestApplication) {
    this.$on('beforeExit', async () => {
      await app.close();
    });
}

上記をこれに置き換える

async enableShutdownHooks(app: INestApplication) {
    process.on('beforeExit', () => {
      app.close();
    });
  }

関連記事