nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

Used in the nestjs (typescript) framework, the ts compiler tells you that the types do not match #10600

Closed ufec closed 1 year ago

ufec commented 1 year ago

Is there an existing issue for this?

Current behavior

Used in the nestjs (typescript) framework, the ts compiler tells you that the types do not match image as shown in the picture I have to use type assertion like this

type FastifyAdapterParameters = Parameters<typeof adapter.register>;
type FastifyAdapterRegister = FastifyAdapterParameters[0];
const Helmet = fastifyHelmet as unknown as FastifyAdapterRegister;
await app.register(Helmet); // or await adapter.register(Helmet);

If I don't do this, I will get a type validation error

Argument of type 'FastifyHelmet' is not assignable to parameter of type 'FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'. Type 'FastifyHelmet' is not assignable to type 'FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault>'.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-yffp2u?file=src/main.ts

Steps to reproduce

npm i -g @nestjs/cli
nest new test_helmet
cd test_helmet
npm uninstall @nestjs/platform-express && npm install --save @nestjs/platform-fastify fastify @fastify/helmet

modify src/main.ts to

import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import fastifyHelmet from '@fastify/helmet';
import { AppModule } from './app.module';

async function bootstrap() {
  const adapter = new FastifyAdapter();
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    adapter,
  );
  app.register(fastifyHelmet);
  await app.listen(3000);
}
bootstrap();

you will get a type error

image

Expected behavior

like this app.register(fastifyHelmet); and no type error

Package

Other package

fastify

NestJS version

No response

Packages versions

{
  "name": "nest-typescript-starter",
  "private": true,
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@fastify/helmet": "^10.1.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-fastify": "^9.2.0",
    "fastify": "^4.10.2",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.5.5"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "^28.1.4",
    "@types/node": "^18.0.3",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.30.5",
    "@typescript-eslint/parser": "^5.30.5",
    "eslint": "^8.19.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^28.1.2",
    "prettier": "^2.7.1",
    "source-map-support": "^0.5.21",
    "supertest": "^6.2.4",
    "ts-jest": "^28.0.5",
    "ts-loader": "^9.3.1",
    "ts-node": "^10.8.2",
    "tsconfig-paths": "^4.0.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

16.17.0

In which operating systems have you tested?

Other

I tried looking for a solution in the fastify-helmet repository, but I realized that I seemed to be looking in the wrong place, because the default fastify works fine Later I forked the nestjs project and manually changed the fastify version locally. After building, I got the expected results in 10-fastify. Should I submit a PR?

ufec commented 1 year ago

I also had the same problem with @fastify/compress

micalevisk commented 1 year ago

I guess upgrading fastify would fix that. #10583

ufec commented 1 year ago

I guess upgrading fastify would fix that. #10583

in my given minimum reproduction code

I use latest fastify, but this is due to packages/platform-fastify/package.json The version of fastify that this file depends on is too low. I tried to submit a PR #10601 but it was closed

micalevisk commented 1 year ago

but it was closed

https://github.com/nestjs/nest/pull/10601#issuecomment-1327965186

ufec commented 1 year ago

but it was closed

#10601 (comment)

When can it be applied

micalevisk commented 1 year ago

when Kamil is available to review and approve it.

ufec commented 1 year ago

when Kamil is available to review and approve it.

OK