express-basic-auth
express-basic-auth copied to clipboard
Typescript example fails
The provided typescript usage produces a typescript error:
import * as basicAuth from 'express-basic-auth'
app.use(basicAuth(options), (req: basicAuth.IBasicAuthedRequest, res, next) => {
res.end(`Welcome ${req.auth.user} (your password is ${req.auth.password})`)
next()
})
Produces this error:
This expression is not callable. Type 'typeof expressBasicAuth' has no call signatures.ts(2349) service.ts(6, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.
Huh, that should work. That always worked like this, not only for this library.
Can you tell us what's your TypeScript version, configuration (tsconfig.json), and try with a default import (import basicAuth from 'express-basic-auth')?
Check to see if you have "esModuelInterop":true enabled in your tsconfig.json file. I ran into the same error and disabling this setting resolved the error.
Same issue but for standard Exrpess:
app.use(
passport.authenticate('oauth-bearer', { session: false }),
(next) => {
next()
}
)
Throws the TS error:

Tried the suggestion "esModuelInterop":false but no luck.
Current workaround:
app.use(
passport.authenticate('oauth-bearer', { session: false }),
(next: CallableFunction) => {
next()
}
)
Another option is to start the unused variables with an underscore:
app.use(
passport.authenticate('oauth-bearer', { session: false }),
(_req, _res, next) => {
next()
}
)
TypeScript version 3.9.7
The provided typescript usage produces a typescript error:
import * as basicAuth from 'express-basic-auth' app.use(basicAuth(options), (req: basicAuth.IBasicAuthedRequest, res, next) => { res.end(`Welcome ${req.auth.user} (your password is ${req.auth.password})`) next() })Produces this error:
This expression is not callable. Type 'typeof expressBasicAuth' has no call signatures.ts(2349) service.ts(6, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.
For me basicAuth.default has solved the issue:
import * as basicAuth from 'express-basic-auth';
...
app.use(basicAuth.default({
users: {
uname: 'secret',
},
challenge: true,
}));
Fails for me as well. Can't get basicAuth.IBasicAuthedRequest to work with requests
I did not maintain the package for a while (simply did not have time), but I recently returned to open source work. I will revive my TS rewrite branch next week and make sure to release a new version written in Typescript until the end of the month, as a lot of people still depend on this package.
Thank you for the patience and activity everyone :-)
I did not maintain the package for a while (simply did not have time), but I recently returned to open source work. I will revive my TS rewrite branch next week and make sure to release a new version written in Typescript until the end of the month, as a lot of people still depend on this package.
Thank you for the patience and activity everyone :-)
Any update here, @LionC ?