middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing type-utils.d.ts file in @middy/util package

lbevilacqua opened this issue · comments

Describe the bug
When importing @middy/util in a TypeScript project, tsc fails with the following error

node_modules/@middy/util/index.d.ts:3:88 - error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

3 import { ArrayValues, Choose, DeepAwaited, IsUnknown, SanitizeKey, SanitizeKeys } from './type-utils'

if I correct the import by adding the .js extension, then tsc fails with another error

node_modules/@middy/util/index.d.ts:3:88 - error TS2307: Cannot find module './type-utils.js' or its corresponding type declarations.

3 import { ArrayValues, Choose, DeepAwaited, IsUnknown, SanitizeKey, SanitizeKeys } from './type-utils.js'

In fact, the type-utils.d.ts file is not present.

To Reproduce
How to reproduce the behaviour:

See sample at https://github.com/lbevilacqua/middy-util-issue

Expected behaviour
tsc command should not produce the above errors.

Environment (please complete the following information):

  • Node.js: 18.18.0
  • Middy: 5.0.0

Additional context
package.json type: module
tsconfig.json module: node16

Thanks for reporting. I see the issue, file wasn't included in the package. I can push an update shortly.

v5.0.1 now releasing. Please let us know if that resolves the issue.

Thank you for the prompt reply.

I updated my test repository using the newly released version.
However, the first error is still present.

Since type-utils is only a type declaration, you could fix the issue by importing it using the type modifier and .d.ts file extension, as in

import type { ArrayValues, Choose, DeepAwaited, IsUnknown, SanitizeKey, SanitizeKeys } from './type-utils.d.ts'

This way it is explicit that we are only importing types and not any underlying javascript code.

When running the tests after updating the file with the .d.ts it errors out: A declaration file cannot be imported without import type. Did you mean to import an implementation file ./type-utils instead?.

cc @lmammino

When running the tests after updating the file with the .d.ts it errors out: A declaration file cannot be imported without import type. Did you mean to import an implementation file ./type-utils instead?.

cc @lmammino

Did you add the type import modifier (see #1136 (comment))?

I did not (I don't use TypeScript myself). Looks like other tests start failing once I do. We'll have to take a deeper look when we can. You're also welcome to put a PR together.

ref: https://github.com/middyjs/middy/tree/feature/ts-import-fix

I see that now the type import modifier has been added to the branch. @lbevilacqua, would you be so kind to test if the changes in Will's branch solve the issue?

Thank you.

I tried copying the modified file over the existing one and I'm not experiencing the issue anymore.