Typescript import issues
nodehack opened this issue · comments
What's the preferred way of importing this module? So far I'm not able to import it so that I can use the koaSwagger function and get typings. I've tried:
import * as koaSwagger from 'koa2-swagger-ui';
results in:
TS2349: Cannot invoke an expression whose type lacks a call signature.
import koaSwagger from 'koa2-swagger-ui';
I get typings but I get the runtime error:
TypeError: koa2_swagger_ui_1.default is not a function
const koaSwagger = require('koa2-swagger-ui');
Works but I get no typings.
import { koaSwagger } from 'koa2-swagger-ui';
Cannot do this because the module has no export member named koaSwagger.
import koaSwagger from 'koa2-swagger-ui';
is working for me. maybe make sure you have "esModuleInterop": true,
in your tsconfig.
here's what i'm using
{
"compileOnSave": false,
"compilerOptions": {
"moduleResolution": "node",
"lib": ["es2018"],
"target": "es2017",
"module": "commonjs",
"esModuleInterop": true,
"strict": true,
"noImplicitAny": false,
"noUnusedLocals": true,
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"outDir": "dist"
},
"include": ["src/index.ts"]
}
Thanks Scott!
Adding "esModuleInterop": true
worked but I had to update a bunch of other imports to use that style. Sounds like a discussion on requiring esModuleInterop could end in a holy war so I'll just take my win and run.
Thanks again!
It’s probably because I tried to keep backwards comparability with the previous nodejs version. Oh well
Okay @nodehack (and others), here's a workaround that allows you to include the library
- saves you from having to turning on
esModuleInterop
(which is great since that messes up every other import) - it also gets you the types
import { KoaSwaggerUiOptions } from 'koa2-swagger-ui';
type koa2SwaggerUiFunc = (config: Partial<KoaSwaggerUiOptions>) => Koa.Middleware
// tslint:disable-next-line: no-var-requires // We actually have to use require for koa2-swagger-ui
const koaSwagger = require('koa2-swagger-ui') as koa2SwaggerUiFunc;