Skip to content
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

aws-lambda: any return types don't agree with strict ESLint rules #32025

Open
1 task
mfulton26 opened this issue Nov 5, 2024 · 3 comments
Open
1 task

aws-lambda: any return types don't agree with strict ESLint rules #32025

mfulton26 opened this issue Nov 5, 2024 · 3 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@mfulton26
Copy link

Describe the bug

In some places aws-cdk returns any but this causes trouble for projects that forbid usage of any (e.g. via ESLint rules).

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

Returns types are properly typed so as to not trigger any any assignment lint warnings/errors.

Current Behavior

Some APIs return any.

Reproduction Steps

    lambda.addEventSource(
        new DynamoEventSource(table, {
            startingPosition: StartingPosition.LATEST,
            filters: [
                FilterCriteria.filter({
                    eventName: FilterRule.isEqual('EVENT_NAME'),
                    userIdentity: {
                        type: FilterRule.isEqual('TYPE'),
                        principalId: FilterRule.isEqual('PRINCIPAL_ID'),
                    },
                }),
            ],
        }),
    );

the usages of FilterRule.isEqual() are flagged with the following error:

Unsafe assignment of an any value. eslint@typescript-eslint/no-unsafe-assignment

Possible Solution

Change the following in event-source-filter.d.ts from...

    static isEqual(item: string | number | boolean): any;

to...

    static isEqual(item: string): [string];
    static isEqual(item: number): [{ numeric: ['=', number] }];
    static isEqual(item: boolean): [boolean];

Additional Information/Context

No response

CDK CLI Version

2.161.0 (build be5ad8b)

Framework Version

No response

Node.js Version

v20.18.0

OS

macOS Sonoma 14.7.1 (23H222)

Language

TypeScript

Language Version

5.6.2

Other information

No response

@mfulton26 mfulton26 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 5, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Nov 5, 2024
@ashishdhingra ashishdhingra self-assigned this Nov 5, 2024
@ashishdhingra ashishdhingra added p2 needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels Nov 5, 2024
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Nov 5, 2024

@mfulton26 Thanks for opening the issue. Please share the your eslint.config.mjs and preferably minimal CDK stack code to reproduce the issue. With the below default eslint.config.mjs, the issue is not reproducible:

import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


/** @type {import('eslint').Linter.Config[]} */
export default [
  {files: ["**/*.{js,mjs,cjs,ts}"]},
  {languageOptions: { globals: globals.browser }},
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
];

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p3 and removed p2 labels Nov 5, 2024
@mfulton26
Copy link
Author

The @typescript-eslint/no-unsafe-assignment rule needs to be enabled.

Here are some TypeScript ESLint Playground links demonstrating the issue and possible fix:

  1. error
  2. possible fix

I can create a more in-depth example if still needed but here are better steps to reproduce then I initially provided:

  1. set up a new TypeScript project with typescript-eslint
  2. set the @typescript-eslint/no-unsafe-assignment to "error"
  3. import FilterRule from aws-cdk-lib/aws-lambda
  4. use FilterRule.isEqual() in any assignment (e.g. const eventName = FilterRule.isEqual("EVENT_NAME");)

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Nov 6, 2024
@ashishdhingra
Copy link
Contributor

Reproducible after adding "@typescript-eslint/no-unsafe-assignment": "error" to eslint.config.mjs file:

export default [
  {files: ["**/*.{js,mjs,cjs,ts}"]},
  {languageOptions: { globals: globals.browser }},
  pluginJs.configs.recommended,
  ...tseslint.configs.recommendedTypeChecked,
  {
    rules: {
      "@typescript-eslint/no-unsafe-assignment": "error",
    },
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname,
      }
    }
  }
];

It flags below error in VS Code IDE:
Screenshot 2024-11-07 at 10 20 09 AM

@ashishdhingra ashishdhingra added effort/medium Medium work item – several days of effort and removed needs-reproduction This issue needs reproduction. labels Nov 7, 2024
@ashishdhingra ashishdhingra removed their assignment Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

2 participants