Skip to content

Fix Typescript import error: TS1192 module has no default export#2230

Merged
abernix merged 7 commits into
apollographql:masterfrom
Arthie:patch-1
Feb 20, 2019
Merged

Fix Typescript import error: TS1192 module has no default export#2230
abernix merged 7 commits into
apollographql:masterfrom
Arthie:patch-1

Conversation

@Arthie

@Arthie Arthie commented Jan 26, 2019

Copy link
Copy Markdown
Contributor

I get the following error when using the apollo-server-core with typescript (3.2.4) :

node_modules/apollo-server-core/dist/types.d.ts:4:8 - error TS1192: Module '"/node_modules/@types/ws/index"' has no default export.
import WebSocket from 'ws';

The change fixes this error!

TODO:

  • Update CHANGELOG.md with your change (include reference to issue & this PR)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass

I get the following error when using the apollo-server-core with typescript (3.2.4) :

node_modules/apollo-server-core/dist/types.d.ts:4:8 - error TS1192: Module '"/node_modules/@types/ws/index"' has no default export.
import WebSocket from 'ws';

The change fixes this error!
@apollo-cla

Copy link
Copy Markdown

@Arthie: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/

@abernix

abernix commented Feb 5, 2019

Copy link
Copy Markdown
Member

@Arthie I do believe we need to provide a fix here, but can you provide a use-case/code-snippet showing exactly how you're encountering this type error, in usage of apollo-server-*, for posterity's sake? Are you using something from apollo-server-core?

@Arthie

Arthie commented Feb 19, 2019

Copy link
Copy Markdown
Contributor Author

@abernix

Just importing ApolloServer from apollo-server-lambda in a file is enough to trigger the error:
import { ApolloServer } from "apollo-server-lambda";

Here is my typescript config:

{
  "compilerOptions": {
    "lib": ["es2017", "esnext.asynciterable"],
    "moduleResolution": "node",
    "noEmit": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "allowUnreachableCode": false,
    "strict": true,
    "sourceMap": true,
    "target": "es2017",
    "outDir": "dist",
    "baseUrl": "src",
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.graphql"]
}

I also get a secondary error similar to the first one but in apollo-server-lambda:

node_modules/apollo-server-lambda/dist/ApolloServer.d.ts:1:8 - error TS1192: Module '".../node_modules/@types/aws-lambda/index"' has no default export.

1 import lambda from 'aws-lambda';

@abernix abernix added the 🧬 typings Relates to TypeScript changes or improvements. label Feb 19, 2019
Fix lambda types import: aws-lambda types has no default export
@Arthie

Arthie commented Feb 19, 2019

Copy link
Copy Markdown
Contributor Author

@abernix Added a new commit to fix the secondary import error. The aws-lamda types package also has no default export.

Summery of this tiny patch:
Fixes 2 import errors: error TS1192: module ... has no default export
import from package ws in apollo-server-core and aws-lambda in apollo-server-lambda

@Arthie Arthie changed the title Fix Typescript import error: TS1192 WebSocket has no default export Fix Typescript import error: TS1192 module has no default export Feb 19, 2019
We've generally moved away from the star-import pattern which TypeScript
also no longer generates with its default `tsc --init` configuration, so it
seems to make sense to keep picking exactly what we want since this pattern
is no longer forced upon us by TypeScript.
While it would be perfectly fine to use `import *` in this particular case,
we've moved away from the star-import pattern in this repository, instead
preferring the `esModuleInterop` compiler option which seems to be the
direction that TypeScript is moving as it's now the default with `tsc --init`.

TypeScript clearly lost its battle with `import *` which many consider to
have been an incorrect direction in the first place.  Some build tools won't
work with star imports, and using them can make some optimizations more
difficult.  By avoiding this pattern here, hopefully we'll avoid moving back
in that direction inadvertently.

Here's a read I found at one point which roughly explains:

https://itnext.io/great-import-schism-typescript-confusion-around-imports-explained-d512fc6769c2

@abernix abernix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a couple follow-up commits to avoid using star-imports, since we've generally moved toward esModuleInterop patterns (which is the default with new tsc --init projects), but this looks great. Thanks for opening this. Hopefully we can get a release cut soon to validate the success.

@abernix abernix merged commit adecfc8 into apollographql:master Feb 20, 2019
@abernix

abernix commented Feb 20, 2019

Copy link
Copy Markdown
Member

This has been included in 2.4.3. I plan on promoting it to the latest tag tomorrow, but you can try this right now using the next tag or by directly installing 2.4.3. e.g.:

npm install apollo-server-lambda@2.4.3

@Arthie

Arthie commented Feb 21, 2019

Copy link
Copy Markdown
Contributor Author

Tested version apollo-server-lambda@2.4.3 and got no errors! Thank you 👍

@abernix

abernix commented Feb 21, 2019

Copy link
Copy Markdown
Member

Thank you for this first contribution!

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

🧬 typings Relates to TypeScript changes or improvements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants