Coder Social home page Coder Social logo

TypeScript unable to infer types about middy HOT 4 CLOSED

canassa avatar canassa commented on August 23, 2024
TypeScript unable to infer types

from middy.

Comments (4)

bilalq avatar bilalq commented on August 23, 2024 1

Summary

It looks like this is a regression in Middy 5, maybe. I still use Middy 4 due to that being the latest version supported by AWS Powertools, so I never noticed this problem. Also, the example in the docs has a minor bug where it references event.path instead of req.path.

Reproduction Steps

This differs a bit from the example shared in the issue description because the tsconfig there gave me ESM problems that weren't really related to the problem at hand. Maybe I needed to set module: true in package.json or something, but I just can't be bothered to deal with any more ESM related problems in my life, so here's a simplified reproduction that sidesteps that:

Create files

package.json:

{
  "name": "middy-example",
  "version": "1.0.0",
  "dependencies": {
    "@middy/core": "^5.4.3",
    "@middy/secrets-manager": "^5.4.3",
    "@types/aws-lambda": "^8.10.141",
    "@types/node": "^20.14.10",
    "typescript": "^5.5.3"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "esModuleInterop": true,
    "strict": true,
    "skipLibCheck": true
  }
}

index.ts:

import middy from '@middy/core'
import secretsManager from '@middy/secrets-manager'
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'

export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>()
  .use(
    secretsManager({
      fetchData: {
        apiToken: 'dev/api_token',
      },
      awsClientOptions: {
        region: 'us-east-1',
      },
      setToContext: true,
    })
  )
  .handler(async (req, context) => {
    // The context type gets augmented here by the secretsManager middleware.
    // This is just an example, obviously don't ever log your secret in real life!
    console.log(context.apiToken)
    return {
      statusCode: 200,
      body: JSON.stringify({
        message: `Hello from ${req.path}`,
        req,
      }),
    }
  })

Run tsc to check types

Command:

npm install && npx tsc --noEmit --pretty
index.ts:17:19 - error TS7006: Parameter 'req' implicitly has an 'any' type.

17   .handler(async (req, context) => {
                     ~~~

index.ts:17:24 - error TS7006: Parameter 'context' implicitly has an 'any' type.

17   .handler(async (req, context) => {
                          ~~~~~~~


Found 2 errors in the same file, starting at: index.ts:17

Workaround

Downgrading to Middy 4 makes it work:

{
  "name": "middy-example",
  "version": "1.0.0",
  "dependencies": {
    "@middy/core": "^4.7.0",
    "@middy/secrets-manager": "^4.7.0",
    "@types/aws-lambda": "^8.10.141",
    "@types/node": "^20.14.10",
    "typescript": "^5.5.3"
  }
}

from middy.

canassa avatar canassa commented on August 23, 2024

Thanks!

I created a separate PR to fix the docs.

from middy.

willfarrell avatar willfarrell commented on August 23, 2024

merged, and docs rebuilding now

from middy.

canassa avatar canassa commented on August 23, 2024

@willfarrell The PR I opened only addresses the documentation. However, this regression issue remains unresolved.

from middy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.