我正在使用 NestJS 构建后端应用程序,并且尝试使用 fastify 而不是 express 作为底层框架。我按照文档(可在此处)使用 fastify,但是我遇到了以下类型问题:
Type 'NestFastifyApplication' does not satisfy the constraint 'INestApplication'.
Type 'NestFastifyApplication' is missing the following properties from type
'INestApplication': use, enableCors, enableVersioning, listenAsync, and 22 more
这里是代码:
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.listen(3001);
}
bootstrap();
问题很简单。安装
@nestjs/platform-fastify
期间出现问题。只需删除并安装软件包就足以解决
更新(感谢@JEFF)
基本上确保 package.json 中的 @nestjs/common、@nestjs/core 和 @nestjs/platform-{express/fastify} 版本匹配(reference)。
PS:如果您有以下问题:
Type 'NestFastifyApplication<RawServerDefault>' does not satisfy the constraint 'INestApplication'.
也许@nestjs/platform 都存在兼容性版本问题- fastify 和 NestJS 核心包,所以运行
yarn upgrade-interactive --latest
将依赖项更新到最新的稳定版本。
参考:https://dev.to/ouelle/basic-crud-operations-with-nestjs-typescript-fastify-mongodb-1f1k