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

Typescript definition generation for resources.Resources has a compilation error due to Fn::Transform #27

Open
remcoabc opened this issue Feb 10, 2021 · 17 comments
Labels
bug Something isn't working

Comments

@remcoabc
Copy link

Whenever I use the intended AWS type (as described in the readme) it throws the following compilation error:
TS2411: Property '"Fn::Transform"' of type '{ Name: string; Parameters?: { [k: string]: unknown; }; }' is not assignable to string index type '{ Type: string; Properties?: { [k: string]: unknown; }; CreationPolicy?: { [k: string]: unknown; }; DeletionPolicy?: string; DependsOn?: string[]; Metadata?: { [k: string]: unknown; }; UpdatePolicy?: { [k: string]: unknown; }; UpdateReplacePolicy?: string; Condition?: string; }'.

@fredericbarthelet
Copy link
Contributor

Hi @remcoabc and thanks for submitting this issue. Could you please provide your serverless.ts file :) ?

@remcoabc
Copy link
Author

remcoabc commented Feb 10, 2021

import { AWS } from '@serverless/typescript';
import { helloWorld } from './functions';

const serverlessConfiguration: AWS = {
  service: 'remco-stage',
  frameworkVersion: '2',
  plugins: [
    'serverless-webpack',
    'serverless-pseudo-parameters',
  ],
  custom: {
    webpack: {
      webpackConfig: './webpack.config.js',
      includeModules: true,
      keepOutputDirectory: true,
    },
  },
  provider: {
    name: 'aws',
    runtime: 'nodejs12.x',
    apiGateway: {
      shouldStartNameWithService: true,
    },
  },
  functions: {
    helloWorld,
  },
};

module.exports = serverlessConfiguration;

@fredericbarthelet

@fredericbarthelet
Copy link
Contributor

Thanks @remcoabc. Your service file looks perfectly fine. Could you share as well your node version and your package.json for better understanding (i'm particulary interested in your TypeScript version). Could you let me know as well your tsconfig.json for typescript specific configuration ? Thanks :)

@remcoabc
Copy link
Author

"dependencies": {
    "@dazn/lambda-powertools-logger": "^1.28.1",
    "@dazn/lambda-powertools-pattern-basic": "^1.28.1",
    "aws-sdk": "^2.828.0",
    "axios": "^0.21.1",
    "dotenv": "^8.2.0",
    "serverless-pseudo-parameters": "^2.5.0",
    "source-map-support": "^0.5.10",
    "ulid": "^2.3.0"
  },
  "devDependencies": {
    "@aws-sdk/types": "^3.0.0",
    "@serverless/typescript": "^2.23.0",
    "@types/aws-lambda": "^8.10.17",
    "@types/jest": "^26.0.18",
    "@types/node": "^10.12.18",
    "@types/serverless": "^1.72.5",
    "@typescript-eslint/eslint-plugin": "^4.13.0",
    "@typescript-eslint/parser": "^4.13.0",
    "eslint": "^7.17.0",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-config-airbnb-typescript": "^12.0.0",
    "eslint-plugin-import": "^2.22.1",
    "fork-ts-checker-webpack-plugin": "^3.0.1",
    "jest": "^26.6.3",
    "serverless": "^2.15.0",
    "serverless-appsync-plugin": "^1.4.0",
    "serverless-export-env": "^1.4.0",
    "serverless-iam-roles-per-function": "^3.0",
    "serverless-manifest-plugin": "^1.0.7",
    "serverless-webpack": "^5.2.0",
    "ts-jest": "^26.4.4",
    "ts-loader": "^5.3.3",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.7",
    "webpack": "^4.29.0",
    "webpack-node-externals": "^1.7.2"
  }

I am using node version 12.20.1
@fredericbarthelet

@fredericbarthelet
Copy link
Contributor

Thanks @remcoabc, can you provide your tsconfig.json as well ? My understanding is that your current configuration checks for dependancies as well.
You can bypass this behavior in 2 ways :

  • set "skipLibCheck": true in your compilerOptions in tsconfig.json
  • ignore node_modules folder by setting "exclude": ["node_modules/**/*"] in tsconfig.json

Can you confirm the above solution solves your problem ?

@remcoabc
Copy link
Author

I have the node_modules folder already ignored as you can see in my tsconfig.json:

{
  "compilerOptions": {
    "lib": [
      "es5",
      "es2015",
      "es2016",
      "es2017",
      "es2018",
      "es2019",
      "es2020"
    ],
    "removeComments": true,
    "moduleResolution": "node",
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "sourceMap": true,
    "target": "es2017",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "resolveJsonModule": true
  },
  "include": ["./**/*.ts", "./**/*.js"],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    "_warmup/**/*",
    ".vscode/**/*",
    "dist/**/*"
  ]
}

@grimm2x
Copy link

grimm2x commented Feb 16, 2021

I had the same issue. Updated the AWS.resources.Resources type with ( | ) to resolve it:

    Resources?: ({
      "Fn::Transform"?: {
        ...
      };
    } | {
      [k: string]: {
        ...
      };
    });

@remcoabc
Copy link
Author

I had the same issue. Updated the AWS.resources.Resources type with ( | ) to resolve it:

    Resources?: ({
      "Fn::Transform"?: {
        ...
      };
    } | {
      [k: string]: {
        ...
      };
    });

This will indeed work if I am able to change the files. Problem is that since I use it in my ci/cd pipeline it will be redownloaded. This means that every change I do to the src code of this library does not have any effect. It would be a solution if that is added in this repo but since the index.d.ts file is auto generated it needs to be fixed somewhere else I think.

@grimm2x
Copy link

grimm2x commented Feb 16, 2021

This will indeed work if I am able to change the files. Problem is that since I use it in my ci/cd pipeline it will be redownloaded. This means that every change I do to the src code of this library does not have any effect. It would be a solution if that is added in this repo but since the index.d.ts file is auto generated it needs to be fixed somewhere else I think.

Agreed, it needs to be fixed somewhere upstream. Hopefully this hints at what we're seeing. Just a temporary option to keep typing instead of casting as any.

  functions: {
    helloWorld,
  } as any

@fredericbarthelet
Copy link
Contributor

@grimm2x @remcoabc thanks to both of you for your insights.
A bit of context concerning the appearance of this issue :

@fredericbarthelet fredericbarthelet added the bug Something isn't working label Feb 16, 2021
@mblpz
Copy link

mblpz commented Feb 22, 2021

how/where can i contribute on this issue? aksing because i stumbled upon #12

@fredericbarthelet fredericbarthelet changed the title index.d.ts is invalid Typescript definition generation for resources.Resources has a compilation error due to Fn::Transform Apr 27, 2021
@JustFly1984
Copy link

I've fixed type declarations and created PR #44

@fredericbarthelet
Copy link
Contributor

There currently is a PR ongoing in json-schema-to-typescript that will solve this issue. Do not hesitate to upvote :)

bcherny/json-schema-to-typescript#383

@macsj200
Copy link

macsj200 commented Nov 9, 2021

I'm still running into this issue on @serverless/typescript version 2.65.0.

package.json
{
  "name": "webhooks-serverless",
  "version": "1.0.0",
  "description": "Serverless aws-nodejs-typescript template",
  "main": "serverless.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "engines": {
    "node": ">=14.15.0"
  },
  "dependencies": {
    "@middy/core": "^1.5.2",
    "@middy/http-json-body-parser": "^1.5.2",
    "source-map-support": "^0.5.19"
  },
  "devDependencies": {
    "@serverless/typescript": "^2.65.0",
    "@types/aws-lambda": "^8.10.71",
    "@types/node": "^14.14.25",
    "json-schema-to-ts": "^1.5.0",
    "serverless": "^2.23.0",
    "serverless-webpack": "^5.3.5",
    "ts-loader": "^8.0.15",
    "ts-node": "^9.1.1",
    "tsconfig-paths": "^3.9.0",
    "tsconfig-paths-webpack-plugin": "^3.3.0",
    "typescript": "^4.1.3",
    "webpack": "^5.20.2",
    "webpack-node-externals": "^2.5.2"
  },
  "author": "The serverless webpack authors (https://github.com/elastic-coders/serverless-webpack)",
  "license": "MIT"
}
tsconfig.json
{
  "extends": "./tsconfig.paths.json",
  "compilerOptions": {
    "lib": ["ESNext"],
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "ES2020",
    "outDir": "lib"
  },
  "include": ["src/**/*.ts", "serverless.ts"],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    "_warmup/**/*",
    ".vscode/**/*"
  ],
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}
Error message
node_modules/@serverless/typescript/index.d.ts:1268:7 - error TS2411: Property '"Fn::Transform"' of type '{ Name: string; Parameters?: { [k: string]: unknown; }; }' is not assignable to 'string' index type '{ Type: string; Properties?: { [k: string]: unknown; }; CreationPolicy?: { [k: string]: unknown; }; DeletionPolicy?: string; DependsOn?: AwsResourceDependsOn; Metadata?: { [k: string]: unknown; }; UpdatePolicy?: { ...; }; UpdateReplacePolicy?: string; Condition?: string; }'.

1268       "Fn::Transform"?: {

This error disappears if I add "skipLibCheck": true to tsconfig.json but my understanding is that this is not considered best practice. I can use this workaround for now until bcherny/json-schema-to-typescript#383 lands.

@Woodz
Copy link

Woodz commented Jan 11, 2022

Thanks @remcoabc, can you provide your tsconfig.json as well ? My understanding is that your current configuration checks for dependancies as well. You can bypass this behavior in 2 ways :

  • set "skipLibCheck": true in your compilerOptions in tsconfig.json
  • ignore node_modules folder by setting "exclude": ["node_modules/**/*"] in tsconfig.json

Can you confirm the above solution solves your problem ?

For my situation (listed below), I needed to apply both workarounds (i.e. both skipLibCheck and ignore node_modules) to fix this.

  • "serverless": "^2.57.0",
  • "@serverless/typescript": "^2.70.0",
  • "target": "ES2019",
  • "module": "commonjs",

@AntonOellerer
Copy link

It seems as if this error is still present when running npx tsc in a fresh instance of a aws-nodejs-typescript template based project

@herebebogans
Copy link

Until / ever this gets fixed another good workaround is to use

patch-package

with above

#27 (comment)

workaround in your dependent projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants