You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS+Fastify注册fastify/secure-session时出现类型错误

解决NestJS + Fastify中@fastify/secure-session类型不兼容问题

问题场景

在基于Fastify适配器的NestJS应用中注册@fastify/secure-session中间件时,触发类型不兼容错误,具体代码和错误信息如下:

代码片段

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ConfigService } from '@nestjs/config';
import { ValidationPipe } from '@nestjs/common';
import session from '@fastify/secure-session';
import { session_key } from '@app/keys';

(async () => {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
const config = app.get(ConfigService);
const { port } = config.get('http');
app.useLogger(config.get('loglevels'));
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.enableShutdownHooks();
app.register(session, { secret: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', salt: 'asdawdafowsuFIU' });
await app.listen(port);
})();

错误信息

Argument of type 'FastifySecureSession' is not assignable to parameter of type 'FastifyPluginCallback<SecureSessionPluginOptions | (SecureSessionPluginOptions & Required<Pick<SecureSessionPluginOptions, "sessionName">>)[]> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.
  Type 'FastifySecureSession' is not assignable to type 'FastifyPluginCallback<SecureSessionPluginOptions | (SecureSessionPluginOptions & Required<Pick<SecureSessionPluginOptions, "sessionName">>)[]>'.
    Types of parameters 'instance' and 'instance' are incompatible.
      Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>' is missing the following properties from type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>': serializeCookie, parseCookie, createSecureSession, decodeSecureSession, and 3 more.
    14 |   app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
    15 |   app.enableShutdownHooks();
  > 16 |   app.register(session, { key: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', salt: 'asdawdafowsuFIU' });
       |                ^^^^^^^
    17 |   await app.listen(port);
    18 | })();
    19 |

解决方案

1. 对齐Fastify相关包版本

类型不兼容的核心原因通常是@fastify/secure-session与NestJS集成的fastify版本不匹配:

  • 查看@nestjs/platform-fastify的peer依赖版本:打开node_modules/@nestjs/platform-fastify/package.json,找到fastify的版本范围。
  • 安装对应兼容版本的@fastify/secure-session,例如Nest依赖fastify@4.x时,需安装@fastify/secure-session@7.x版本(具体兼容关系参考插件官方文档)。
  • 执行安装命令:
npm install @fastify/secure-session@<兼容版本号>
# 或使用yarn
yarn add @fastify/secure-session@<兼容版本号>

2. 修正插件导入方式

部分版本的@fastify/secure-session需要使用命名导入或全量导入,替换原导入语句:

import * as session from '@fastify/secure-session';

3. 直接操作底层Fastify实例

绕过Nest封装的类型校验,直接获取Fastify原生实例进行插件注册:

const fastifyInstance = app.getHttpAdapter().getInstance();
await fastifyInstance.register(session, { 
  secret: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', 
  salt: 'asdawdafowsuFIU' 
});

4. 临时类型断言(不推荐长期使用)

若上述方案无效,可临时通过类型断言绕过类型检查,仅用于调试场景:

app.register(session as any, { 
  secret: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', 
  salt: 'asdawdafowsuFIU' 
});

内容的提问来源于stack exchange,提问作者Andy95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 06:33:17