koajs / koa-body

koa body parser middleware
MIT License
947 stars 131 forks source link

[fix] A TypeScript error occurs when tsc is used to build a project #229

Open F999999999 opened 1 year ago

F999999999 commented 1 year ago

Describe the bug

Node.js version: v18.15.0

OS version: Windows 11

Description: A TypeScript error occurs when configuring eslint+prettier and using tsc to build projects Prettier

Actual behavior

node_modules/.pnpm/@types+koa-bodyparser@4.3.10/node_modules/@types/koa-bodyparser/index.d.ts:24:9 - error TS2717: Subsequent property declarations must have the same type.  Property 'body' must be of type 'any', but here has type 'unknown'.

24         body?: unknown;
           ~~~~

  node_modules/.pnpm/koa-body@6.0.1/node_modules/koa-body/lib/index.d.ts:8:9
    8         body?: any;
              ~~~~
    'body' was also declared here.

Found 1 error in node_modules/.pnpm/@types+koa-bodyparser@4.3.10/node_modules/@types/koa-bodyparser/index.d.ts:24/

Expected behavior

build success

Code to reproduce

.eslintrc.js

module.exports = {
  root: true,
  env: {
    es2022: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
  ],
  overrides: [],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint'],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  },
};

.prettierrc.js

module.exports = {
  singleQuote: true,
};

app.ts (pseudo-code)

import Koa from "koa";
import body from "koa-body";
import http from "http";

const app = new Koa();
app.use(body());
http.createServer(app.callback()).listen(3000);

Checklist

MarkHerhold commented 1 year ago

It works for me. I noticed you didn't include a build command or tsconfig file. Here's my attempted reproduction of your issue:

Run npm run build. Output files are in dist/.

issue-299.zip

F999999999 commented 1 year ago

I compiled with tsc,I've found out why,This is because I installed the @types/koa-bodyparser package causing type conflicts. Delete it and it will be normal,I am very sorry to waste your precious time dealing with this issue,Please close this issue