-
-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript unable to infer types #1223
Comments
SummaryIt 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 Reproduction StepsThis 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 Create filespackage.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 typesCommand: npm install && npx tsc --noEmit --pretty
WorkaroundDowngrading 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"
}
} |
Thanks! I created a separate PR to fix the docs. |
merged, and docs rebuilding now |
@willfarrell The PR I opened only addresses the documentation. However, this regression issue remains unresolved. |
Describe the bug
I’m trying to implement this example from the Middy documentation. I copied it exactly into my project, but when I run it, TypeScript is unable to determine the types of event and context. I believe this example originated from a comment by @bilalq, maybe he knows how to get this to work?
My TS config:
Environment:
The text was updated successfully, but these errors were encountered: