aws-lite icon indicating copy to clipboard operation
aws-lite copied to clipboard

Bug with TS & DynamoDB plugin with TS option `strict` is enabled

Open j0k3r opened this issue 1 year ago • 9 comments

Describe the issue

When using the plugin with TS, I got the error:

error TS7016: Could not find a declaration file for module '@aws-lite/dynamodb'. '/..../node_modules/@aws-lite/dynamodb/src/index.mjs' implicitly has an 'any' type. Try npm i --save-dev @types/aws-lite__dynamodb if it exists or add a new declaration (.d.ts) file containing declare module '@aws-lite/dynamodb';

2 import dynamodb from '@aws-lite/dynamodb'

I've properly updated my tsconfig.json. The bug appear because I've these options defined in the config file:

 "strict": true,
 "noImplicitAny": true,

Enable at least one of those option, raise the above error when running yarn tsc.

Might be the same as https://github.com/architect/aws-lite/issues/70 but maybe without the same option in tsconfig file.

Expected behavior

It compiles without error.

Steps to reproduce

  1. Install both the client & dynamo plugin
  2. Enable TS config with option strict or noImplicitAny set to true
  3. Setup awsLite to use the Dynamo plugin
  4. Launch yarn tsc

Platform / version

  • OS + version: (e.g. iOS 17.2) unimportant
  • Node version: (e.g. Node.js 20.5) v20.10.0
  • Package manager version: (e.g. npm 10.2) 1.22.21
  • Browser: (e.g. Chrome 70.5, if applicable) unimportant
  • TS 4

How urgent do you feel this bug is?

P2

Additional context

No response

j0k3r avatar Feb 08 '24 10:02 j0k3r

Hi @j0k3r thanks for steps to repro. Did you install the @aws-lite/dynamodb-types package? it provides "ambient" types for the .DynamoDB interface.

I think you might have because you mentioned that it's similar to #70 and that resolution was this in the tsconfig

{
  "compilerOptions": {
    "types": [
      "@aws-lite/dynamodb-types"
    ]
  }
}

But I just want to double check.

Also, a new version of aws-lite went out very recently that requires explicit loading of plugins. Are you using @aws-lite/[email protected]?

That'll help me differentiate pre-0.17 vs latest. Thanks

tbeseda avatar Feb 08 '24 16:02 tbeseda

Hey @tbeseda,

Did you install the @aws-lite/dynamodb-types package? it provides "ambient" types for the .DynamoDB interface.

Yes, sorry I didn't mention it. We do have that package:

"@aws-lite/dynamodb-types": "^0.3.5",

that resolution was this in the tsconfig

Yes it's enabled.

{
  "compilerOptions": {
    "incremental": true,
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true,
    "noEmit": true,
    "types": [
      "@aws-lite/dynamodb-types",
      "@types/jest"
    ]
  },
  "include": [
    "src",
    "tests",
    "jest.config.ts",
    "webpack.config.js"
  ],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    ".vscode/**/*"
  ]
}

Are you using @aws-lite/[email protected]?

We are using the latest client:

"@aws-lite/client": "^0.17.2",

This is how we are creating the client:

import awsLite from '@aws-lite/client'
import dynamodb from '@aws-lite/dynamodb'

const awsLiteConfig = {
  region: 'eu-west-1',
  plugins: [dynamodb],
  autoloadPlugins: false,
}
const aws = await awsLite(awsLiteConfig)

Overall, the error is still here.

j0k3r avatar Feb 13 '24 09:02 j0k3r

Thanks for getting back to me and adding more info. I'm working on an update to type defs today, so I can look into this with your configuration today.

tbeseda avatar Feb 13 '24 16:02 tbeseda

hey @tbeseda I am facing same types issue.

Cannot find type definition file for '@aws-lite/sqs-types'. The file is in the program because: Entry point of type library '@aws-lite/sqs-types' specified in compilerOptionsts

Tried

    "typeRoots": ["node_modules/@types", "src/@types", "@aws-lite/sqs-types"],
    "types": ["@aws-lite/sqs-types"],

    "dependencies": {
    "@aws-lite/client": "^0.19.0",
    "@aws-lite/sqs": "^0.2.1",
    
   
    "devDependencies": {
    "@aws-lite/sqs-types": "^0.2.3",
    

imdkbj avatar Mar 24 '24 05:03 imdkbj

Ran into this in my first foray into aws-lite. As an example, @aws-lite/[email protected] has this declaration:

declare module "@aws-lite/client" {
  interface AwsLiteClient {
    SSM: AwsLiteSSM;
  }
}

This is good, it merges SSM stuff into the client. The actual TS error is in the import statement (either import or import()) - there's no declaration for the actual @aws-lite/ssm module.

You can make this go away with:

declare module '@aws-lite/ssm';

It's not clear to me where this should happen - it can be:

  1. up to the consumer, you drop a d.ts file with the above somewhere that TS will pick it up.
  2. in the @aws-lite/ssm package; add a types field in package.json and point it at that one-liner.
  3. in the @aws-lite/ssm-types package; drop that line in index.d.ts somewhere.

It's worth noting that AwsLiteConfig['plugins'] is any[].

keithlayne avatar Apr 08 '24 18:04 keithlayne

Thanks for the diagnostics here @keithlayne - very helpful. I'm taking a look at this today to see how much can be handled automatically by aws-lite and what we should advise consumers do.

tbeseda avatar Apr 08 '24 18:04 tbeseda

Okay finally an update here:

I recreated the issue locally in a fresh TS project with the following config:

{
  "scripts": {
    "build": "tsc"
  },
  "devDependencies": {
    "@aws-lite/ssm-types": "^0.2.5",
    "@types/node": "^20.12.7",
    "typescript": "^5.4.5"
  },
  "dependencies": {
    "@aws-lite/client": "^0.20.0",
    "@aws-lite/ssm": "^0.2.3"
  }
}
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "target": "ES6",
    "types": [
      "@aws-lite/ssm-types"
    ]
  }
}

my working index.ts:

import awsLite from '@aws-lite/client'
import awsLiteSsm from '@aws-lite/ssm'

(async () => {
  const aws = await awsLite({
    plugins: [awsLiteSsm]
  })

  await aws.SSM.GetParameter({
    Name: 'foobar',
    WithDecryption: true,
  })
})()

this required a couple updates to the base aws-lite/client types. namely:

/// <reference types="node" />
// ...
declare module "@aws-lite/client";
export default function awsLite(config: AwsLiteConfig): Promise<AwsLiteClient>;

further, I added a "types" field to aws-lite/ssm's package.json that points to a file that simply does:

declare module '@aws-lite/ssm';

I was not able to use that one-liner in the aws-lite/ssm-types. I get:

Invalid module name in augmentation.
Module '@aws-lite/ssm' resolves to an untyped module at '/node_modules/@aws-lite/ssm/src/index.mjs', 
which cannot be augmented

Ideally, I could get this working by only changing the types package and not adding a file to the plugin. Research continues. Suggestions welcome!

tbeseda avatar Apr 15 '24 19:04 tbeseda

Any update on this. Just looking at trying aws-lite for dynamodb usage and encountered this exact issue.

agawley avatar Aug 16 '24 13:08 agawley

Having the same issue here. I think there is a mistake somewhere, but I'm still unable to figure out what.

To see this issue in action: https://stackblitz.com/edit/stackblitz-starters-u8a5t1 (execute npm run build from the terminal)

The setup is also a bit unusual. My suggestion would be to publish the types in the https://github.com/DefinitelyTyped/DefinitelyTyped repo instead. Or start rewriting this SDK to TypeScript, so that the typings can be included in the same packages as the source code.

A workaround is to add a file, e.g. @aws-lite-dynamodb.d.ts, containing:

declare module '@aws-lite/dynamodb' {
    export * from '@aws-lite/dynamodb-types'
}

dirkluijk avatar Sep 19 '24 12:09 dirkluijk