修复无服务器打字稿,属性 'event' 的类型不兼容?

Fixing serverless typescript, Types of property 'event' are incompatible?

我已经升级到无服务器 3,运行 typescript。无服务器配置文件中存在类型匹配错误。

部分我的 serverless.ts 文件是

const serverlessConfiguration: Serverless = {
service: 'hello',
frameworkVersion: '3',
useDotenv: true,

plugins: ['serverless-webpack', 'serverless-domain-manager', 'serverless-iam-roles-per-function', 'serverless-certificate-creator'],
provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    region: 'ap-southeast-2',
    apiGateway: {
        minimumCompressionSize: 1024,
        shouldStartNameWithService: true,
    },
    environment: {
        AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
    },
},
functions: {hello},
custom: {

hello/index.ts 文件的架构有问题

import schema from './schema';
import { handlerPath } from '@libs/handlerResolver';

export default {
  handler: `${handlerPath(__dirname)}/handler.main`,
  events: [
   {
     http: {
       method: 'post',
       path: 'hello',
       private: false,
       request: {
         schemas: {
           'application/json': schema
        }
      }
    }
  }
 ]

}

我读到 http.request.schema 已被 http.request.schemas

取代

我收到的错误消息是

  Type '{ schemas: { 'application/json': { readonly type: "object"; readonly properties: { readonly name: { readonly type: "string"; }; }; readonly required: readonly ["name"]; }; }; }' has no properties in common with type 'HttpRequestValidation'.

包@types/serverless 规格有误

删除这个包

npm remove @types/serverless 

添加无服务器打字包

npm i -D @serverless/typescript

将 serverless.ts 中的导入更改为

import type {Serverless} from 'serverless/aws';

import type {AWS} from "@serverless/typescript";

确保 index.tsscheams 而不是 schema

最终的index.ts文件如上

Ref