typescript icon indicating copy to clipboard operation
typescript copied to clipboard

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

Open remcoabc opened this issue 3 years ago • 17 comments

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; }'.

remcoabc avatar Feb 10 '21 13:02 remcoabc

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

fredericbarthelet avatar Feb 10 '21 13:02 fredericbarthelet

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

remcoabc avatar Feb 10 '21 14:02 remcoabc

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 :)

fredericbarthelet avatar Feb 14 '21 11:02 fredericbarthelet

"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

remcoabc avatar Feb 15 '21 10:02 remcoabc

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 ?

fredericbarthelet avatar Feb 16 '21 09:02 fredericbarthelet

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/**/*"
  ]
}

remcoabc avatar Feb 16 '21 09:02 remcoabc

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

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

grimm2x avatar Feb 16 '21 10:02 grimm2x

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.

remcoabc avatar Feb 16 '21 12:02 remcoabc

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

grimm2x avatar Feb 16 '21 20:02 grimm2x

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

  • type checking started failing at release v2.24.0 of serverless with the following addition https://github.com/serverless/serverless/pull/8929 If you'd like to fix this for the moment, you can revert back to the previous tsconfig.json where serverless.ts is not part of the included glob pattern

  • index.d.ts does includes a mistake at the time being. This is due to the way json-schema-to-typescript handles additionalProperties definition from JSON Schema : while this keyword is used to enforce types on properties not defined in the original list of propertiess, TS enforces usage of index signature on all defined properties. This issue is currently under discussion in https://github.com/bcherny/json-schema-to-typescript/issues/356#issuecomment-748529648 - would you want to help infer in the way of generating union types, your help would be greatly appreciated :)

fredericbarthelet avatar Feb 16 '21 22:02 fredericbarthelet

how/where can i contribute on this issue? aksing because i stumbled upon https://github.com/serverless/typescript/pull/12

mblpz avatar Feb 22 '21 11:02 mblpz

I've fixed type declarations and created PR https://github.com/serverless/typescript/pull/44

JustFly1984 avatar Apr 28 '21 05:04 JustFly1984

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

https://github.com/bcherny/json-schema-to-typescript/pull/383

fredericbarthelet avatar May 04 '21 06:05 fredericbarthelet

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 https://github.com/bcherny/json-schema-to-typescript/pull/383 lands.

macsj200 avatar Nov 09 '21 21:11 macsj200

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",

Woodz avatar Jan 11 '22 08:01 Woodz

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

AntonOellerer avatar Oct 31 '22 15:10 AntonOellerer

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

patch-package

with above

https://github.com/serverless/typescript/issues/27#issuecomment-779755994

workaround in your dependent projects

herebebogans avatar Nov 17 '23 00:11 herebebogans