midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.4k stars 576 forks source link

midwayjs v3 使用了@midwayjs/upload组件 和 fliter @catch 修饰器后 或有类型冲突 #3062

Closed mynamexjl closed 1 year ago

mynamexjl commented 1 year ago

configuration.ts

import { App, Configuration, Inject } from '@midwayjs/decorator'; import { ILifeCycle, IMidwayContainer, MidwayDecoratorService, } from '@midwayjs/core'; // eslint-disable-next-line node/no-extraneous-import import { Application } from 'egg'; import { join } from 'path'; import as oss from '@midwayjs/oss'; import as redis from '@midwayjs/redis'; import as axios from '@midwayjs/axios'; import as validate from '@midwayjs/validate'; import as midwayAuto from '@ali/midway-auto'; // import as typegoose from '@midwayjs/typegoose'; import as swagger from '@midwayjs/swagger'; import as keycenter from '@ali/midway-keycenter'; import { MethodDecorators } from './decorator'; import as bull from '@midwayjs/bull'; import as bullBoard from '@midwayjs/bull-board'; import as orm from '@midwayjs/typeorm'; import { DbInitService } from './service/DbInit'; import as upload from '@midwayjs/upload'; import * as crossDomain from '@midwayjs/cross-domain'; import { MidwayErrorFilter, OtherErrorFilter, ValidateErrorFilter, } from './filter/error';

@Configuration({ imports: [ upload, midwayAuto, oss, redis, // 导入 redis 组件 axios, // http请求 validate, // 参数校验 keycenter, // 加载 keycenter 组件 // typegoose, // 使用mongodb 数据库需要打开这个 bull, // 任务队列 bullBoard, // bull ui orm, // typeorm crossDomain, // 跨域 { component: swagger, // 加载 swagger 组件 enabledEnvironment: ['local'], }, ], importConfigs: [join(__dirname, './config')], }) export class ContainerLifeCycle implements ILifeCycle { @App() app: Application;

@Inject() decoratorService: MidwayDecoratorService;

@Inject() dbInitService: DbInitService;

/**

fiter/error.ts import { Catch, MidwayError } from '@midwayjs/core'; import { MidwayValidationError } from '@midwayjs/validate'; import { Context } from 'egg'; import { Result } from '../function/Result'; import { RESULT_CODE_ERROR, RESULT_MSG_ERROR, RESULT_SERVICE_CODE_ERROR, } from '../constant/constant';

@Catch(MidwayError) export class MidwayErrorFilter { async catch(err: MidwayError, ctx: Context) { console.log('=== MidwayErrorFilter'); console.log(err); // 框架错误信息不抛出 若是后面需要下发自定义错误码可后面扩展 ctx.body = Result.errMsg(RESULT_MSG_ERROR, RESULT_SERVICE_CODE_ERROR); } }

@Catch(MidwayValidationError) export class ValidateErrorFilter { async catch(err: MidwayError, ctx: Context) { console.log('=== MidwayValidationError'); console.log(err); ctx.body = Result.errMsg(err.message, RESULT_CODE_ERROR); } }

@Catch() export class OtherErrorFilter { async catch(err: Error, ctx: Context) { console.log('=== OtherErrorFilter'); console.log(err); ctx.body = Result.errMsg(err.message, RESULT_CODE_ERROR); } }


启动项目直接会报格式类型错

错误信息:

start error: ⨯ Unable to compile TypeScript: src/configuration.ts(136,7): error TS2322: Type 'typeof MidwayErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'. Construct signature return types 'MidwayErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible. The types of 'catch' are incompatible between these types. Type '(err: MidwayError, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'. Types of parameters 'ctx' and 'ctx' are incompatible. Type 'Context' is not assignable to type 'Context'. The types returned by 'cleanupRequestFiles(...)' are incompatible between these types. Type 'Promise<boolean[]>' is not assignable to type 'Promise'. Type 'boolean[]' is not assignable to type 'void'. src/configuration.ts(137,7): error TS2322: Type 'typeof ValidateErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'. Construct signature return types 'ValidateErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible. The types of 'catch' are incompatible between these types. Type '(err: MidwayError, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'. Types of parameters 'ctx' and 'ctx' are incompatible. Type 'Context' is not assignable to type 'Context'. src/configuration.ts(138,7): error TS2322: Type 'typeof OtherErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'. Construct signature return types 'OtherErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible. The types of 'catch' are incompatible between these types. Type '(err: Error, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'. Types of parameters 'ctx' and 'ctx' are incompatible. Type 'Context' is not assignable to type 'Context'.


是否有解决方案

czy88840616 commented 1 year ago

import { Context } from '@midwayjs/web';

mynamexjl commented 1 year ago

(哈哈哈哈。 上面问题完美解决了哈。 能说下 egg 和。web 的 context的区别吗 )还有个问题 typeorm synchronize 日常同步出现表已创建问题导致程序卡住问题(云 mysql 上开启会存在这个问题 本地上是不会) 我不想把 synchronize 设置 false 在通过 cli 去同步的话

czy88840616 commented 1 year ago

1、包含关系 2、不了解,一般不会开。

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.