16

I want to get the following example code from https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html working, but I get a "Argument of type 'Function' is not assignable to parameter of type 'IFunction'" error.

import * as cdk from '@aws-cdk/core';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';

export default class ApiGatewayFunctionStack extends cdk.Stack {
  
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    
    super(scope, id, props);

    const handler = new lambda.Function(this, 'WidgetHandler', {
      runtime: lambda.Runtime.NODEJS_10_X, // So we can use async in widget.js
      code: lambda.Code.fromAsset('resources'),
      handler: 'widgets.main',
    });

    const api = new apigateway.RestApi(this, 'widgets-api', {
      restApiName: 'Widget Service',
      description: 'This service serves widgets.',
    });

    const getWidgetsIntegration = new apigateway.LambdaIntegration(handler, {
      requestTemplates: { 'application/json': '{ "statusCode": "200" }' },
    });

    api.root.addMethod('GET', getWidgetsIntegration); // GET /
  }
}

The full error below seems to indicate that at least part of the issue migh be that the aws-apigateway package has its own packages that are incompatible.

I am lost as to how to resolve this, so any help is much appreciated.

test-deploy/ApiGatewayFunctionStack.ts:49:68 - error TS2345: Argument of type 'Function' is not assignable to parameter of type 'IFunction'.
  Types of property 'role' are incompatible.
    Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined'.
      Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/role").IRole' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/role").IRole'.
        Types of property 'grant' are incompatible.
          Type '(grantee: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/grant").Grant' is not assignable to type '(grantee: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib...'.
            Types of parameters 'grantee' and 'grantee' are incompatible.
              Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal'.
                Types of property 'addToPolicy' are incompatible.
                  Type '(statement: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean' is not assignable to type '(statement: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean'.
                    Types of parameters 'statement' and 'statement' are incompatible.
                      Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement'.
                        Types have separate declarations of a private property 'action'.

49     const getWidgetsIntegration = new apigateway.LambdaIntegration(handler, {
Andy Balham
  • 163
  • 1
  • 1
  • 4
  • The issue goes away if I delete the /agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules folder. However, `npm i` causes this folder to reappear and cause the issue again. – Andy Balham Mar 10 '21 at 20:15
  • The issue does seem to be that installing the @aws-cdk/aws-apigateway package creates a node_modules sub-folder containing incompatible versions of interfaces. Building on GitHub, the same error occurs and I am lost as to why this node_modules sub-folder is being created and how it might be suppressed. – Andy Balham Mar 10 '21 at 20:35
  • can you add your package.json ? typically happens when cdk dependencies are at different versions. – Balu Vyamajala Mar 11 '21 at 01:10
  • 1
    take off the ^ symbols in package.json for dependencies `"@aws-cdk/aws-lambda": "^1.90.0"` to `"@aws-cdk/aws-lambda": "1.90.0"`, delete package-lock.json, remove node_modules and npm install one more time and try it . – Balu Vyamajala Mar 11 '21 at 01:16
  • Thanks for you suggestions. My package.json can be found here: https://github.com/andybalham/agb-aws-functions/blob/main/package.json. I will try your suggestions later. – Andy Balham Mar 11 '21 at 08:51
  • @BaluVyamajala thank you so much for your advice. Following it has resolved the issue. – Andy Balham Mar 11 '21 at 18:18
  • Awesome, I added an answer, so its helpful if any one faces the same issue. You can help by [accepting](https://meta.stackexchange.com/a/5235/919289) and/or upvoting. Thanks. – Balu Vyamajala Mar 11 '21 at 18:46

2 Answers2

44

This error Argument of type 'SomeClass' is not assignable to parameter of type 'ISomeClass' typically occurs when version of CDK dependencies are at different versions. To solve the issue, we need to bring all the dependencies to same version.

  • Delete node_modules folder
  • Delete package-lock.json
  • Ensure all dependencies in package.json are using same version.
  • Remove carrot ^ symbol before dependencies for example from "@aws-cdk/aws-lambda": "^1.90.0" to "@aws-cdk/aws-lambda": "1.90.0" , to avoid different minor versions getting installed.
  • npm install
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42
  • Many thanks, man, you saved me for days of debugging, I was deleting modules and forgot to delete the package-lock.json – Espoir Murhabazi Jul 23 '21 at 07:51
  • 3
    you save my startup! – Jorge Tovar Aug 10 '21 at 16:35
  • I tried same getting : > npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: cdk@0.1.0 npm ERR! Found: typescript@3.7.7 npm ERR! node_modules/typescript npm ERR! dev typescript@"~3.7.2" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer typescript@">=3.8 <5.0" from ts-jest@27.0.7 npm ERR! node_modules/ts-jest npm ERR! dev ts-jest@"^27.0.7" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry – Ashish Karpe Oct 29 '21 at 06:37
  • npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See /Users/ashishkarpe/.npm/eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! /Users/ashishkarpe/.npm/_logs/2021-10-29T06_36_09_516Z-debug.log – Ashish Karpe Oct 29 '21 at 06:37
  • my detail issue is here https://stackoverflow.com/questions/69732793/aws-cdk-typescript-issue-the-expected-type-comes-from-property-securitygroups?noredirect=1#comment123269657_69732793 – Ashish Karpe Oct 29 '21 at 06:40
  • Thanks, this was really handy for me. I was getting the error `Property 'architecture' is missing in type 'Function' but required in type 'IFunction'.` when trying to integrate `@aws-cdk/aws-apigateway` into my CDK project and I noticed that there was a version mismatch between it and the other services. – jpcaparas Mar 06 '22 at 01:14
  • Just FYI, in the package.json file no need to change the version under `devDependencies`, only changing versions under `dependencies` is enough. – Yeamin Rajeev Mar 23 '22 at 01:54
0

The other solution is to import the package like this :

const lambda = require('@aws-cdk/aws-lambda');

But code quality checkers (e.g. ESLINT) may not like it.

Yeamin Rajeev
  • 175
  • 4
  • 3