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] Definitions prevent importing middy #212

Closed
MichaelHindley opened this issue Jul 10, 2018 · 9 comments
Closed

[TypeScript] Definitions prevent importing middy #212

MichaelHindley opened this issue Jul 10, 2018 · 9 comments

Comments

@MichaelHindley
Copy link

Using middy 0.15.7 and Typescript 2.8.3.

import middy from 'middy' results in module node_modules/middy/index has no default export.

import { middy } from 'middy' results in has no exported member middy, did you mean IMiddy? IMiddy is just an interface and can not be used as a value though.

Changing export = middy; to export default middy in https://github.com/middyjs/middy/blob/master/index.d.ts#L41 fixes this issue

@cryptiklemur
Copy link

import * as middy from 'middy' is what you want

@MichaelHindley
Copy link
Author

MichaelHindley commented Aug 4, 2018

@aequasi while that may work, it results in:

ERROR in /project/node_modules/middy/middlewares.d.ts
(4,8): error TS1192: Module '"/project/node_modules/middy/index"' has no default export.

When using middlewares.

The line is middleware.d.ts#L4 which also expects it to be export default middy rather than export = middy.

Is there a reason why a default export is not used in this situation? It's just a bit weird not being able to import middy from 'middy', which I would say is the default most would expect.

@cryptiklemur
Copy link

What i posted is meant to solve the "no default export". I'm using it now with no issue.

Default exports are a typescript thing. The are the equiv of doing module.exports.default = SomeModule. For non typescript projects, that forces you to require with const SomeModule = require('some-module').default;

@cryptiklemur
Copy link

And, if this type definition was generated automatically (which i'd assume), it wont get a default export

@MichaelHindley
Copy link
Author

I see, aren't default exports are a ES6 thing and not ts specific though?

But middleware.d.ts#L4 is not compatible with the type definitions for middy either way you chose to import- it will fail every time as soon as you use any middleware, I'm not quite sure how you got it to work since just checking out middy in a blank project gives the typescript compiler error in regards to this!

image

@cryptiklemur
Copy link

No, they are ts specific

import {IMiddy} from 'middy';
import * as jsonBodyParser from 'middy/src/middlewares/jsonBodyParser';
import * as httpErrorHandler from 'middy/src/middlewares/httpErrorHandler';

The above works perfect for me, and has the added benefit of not including the other middleware when not necessary.

You can ALSO do:

import {IMidd} from 'middy';
import {jsonBodyParser, httpErrorHandler} from 'middy/middlewares'

@lakshmi-kannan
Copy link

@aequasi Sorry, typescript newbie here

import * as cors from 'middy/src/middlewares/js'; 

results in error

Could not find a declaration file for module 'middy/src/middlewares/cors'. '/home/lakshmi/Work/src/hearout/hearout-app/node_modules/middy/src/middlewares/cors.js' implicitly has an 'any' type.
  Try `npm install @types/middy` if it exists or add a new declaration (.d.ts) file containing `declare module 'middy/src/middlewares/cors';`ts(7016)
Submodule import paths from this package are disallowed; import from the root instead (no-submodule-imports)

whereas

import { cors } from 'middy/middlewares';

results in

Submodule import paths from this package are disallowed; import from the root instead (no-submodule-imports)

Typescript: 3.5.3.

tsconfig.json https://gist.github.com/lakshmi-kannan/b5161d3291efaa99f2d62e6566e56271
tslint.json https://gist.github.com/lakshmi-kannan/127e05fca2c74c4bcb0be6ceb8177d24

Any ideas?

@krlozadan
Copy link

krlozadan commented Oct 30, 2019

When using: import { cors } from 'middy/middlewares'; I'm getting:

<--- Last few GCs --->

[38700:0x102654000]    28343 ms: Scavenge 1383.2 (1423.1) -> 1382.5 (1423.6) MB, 3.6 / 0.0 ms  (average mu = 0.191, current mu = 0.127) allocation failure 
[38700:0x102654000]    28351 ms: Scavenge 1383.2 (1423.6) -> 1382.6 (1424.1) MB, 3.4 / 0.0 ms  (average mu = 0.191, current mu = 0.127) allocation failure 
[38700:0x102654000]    28358 ms: Scavenge 1383.5 (1424.1) -> 1383.1 (1425.6) MB, 4.1 / 0.0 ms  (average mu = 0.191, current mu = 0.127) allocation failure 


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x9ea5715be3d]
    1: StubFrame [pc: 0x9ea571110cb]
Security context: 0x30f7d469e6e9 <JSObject>
    2: scanJsDocToken [0x30f76cd5b389] [/Users/carlos.cortes/Documents/dev/wps-serverless/node_modules/typescript/lib/typescript.js:~8580] [pc=0x9ea571e51f2](this=0x30f76cd4e5d1 <Object map = 0x30f7cc7cb319>)
    3: /* anonymous */(aka /* anonymous */) [0x30f7dff246a1] [/Users/carlos.cortes/Documents/dev/wps-serverless/nod...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x10003c597 node::Abort() [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 2: 0x10003c7a1 node::OnFatalError(char const*, char const*) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 3: 0x1001ad575 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 4: 0x100579242 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 5: 0x10057bd15 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 6: 0x100577bbf v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 7: 0x100575d94 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 8: 0x10058262c v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
 9: 0x1005826af v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
10: 0x100551ff4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
11: 0x1007da044 v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/carlos.cortes/.nvm/versions/node/v10.15.3/bin/node]
12: 0x9ea5715be3d 
13: 0x9ea571110cb 
14: 0x9ea571e51f2 
15: 0x9ea57ba0265 

I'm using:

  • Middy 0.30.4
  • Typescript 3.6.4
{
  "compilerOptions": {
    "forceConsistentCasingInFileNames": true,
    "lib": ["es2015", "es2016"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "outDir": ".build",
    "plugins": [
      { "name": "typescript-tslint-plugin" }
    ],
    "preserveConstEnums": true,
    "rootDir": "./",
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": true,
    "target": "es6",
    "esModuleInterop": true
  },
  "exclude": ["node_modules"]
}

@willfarrell
Copy link
Member

I'm going to close for now. All ts definitions will be redone in middy v2. I will need help, as I don't know TS, please post on #587 if you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants