Reputation: 7260
"use strict"
import passport from "passport"
import { Strategy } from "passport-http-bearer"
passport.use(
"users",
new Strategy({ passReqToCallback: true }, (req, token, done) => {
if (token !== "foo") {
return done(null, false)
}
return done(null, {}, { scope: "user" })
})
)
[ts] src/authenticate.ts(8,46): error TS7006: Parameter 'req' implicitly has an 'any' type.
[ts] src/authenticate.ts(8,51): error TS7006: Parameter 'token' implicitly has an 'any' type.
[ts] src/authenticate.ts(8,58): error TS7006: Parameter 'done' implicitly has an 'any' type.
Types installed using npm install @types/passport-http-bearer
.
// Type definitions for passport-http-bearer 1.0.1
// Project: https://github.com/jaredhanson/passport-http-bearer
// Definitions by: Isman Usoh <https://github.com/isman-usoh>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="passport"/>
/// <reference types="express" />
import passport = require("passport");
import express = require("express");
interface IStrategyOptions {
scope?: string | Array<string>;
realm?: string;
passReqToCallback?: boolean;
}
interface IVerifyOptions {
message?: string;
scope: string | Array<string>;
}
interface VerifyFunction {
(token: string, done: (error: any, user?: any, options?: IVerifyOptions | string) => void): void;
}
interface VerifyFunctionWithRequest {
(req: express.Request, token: string, done: (error: any, user?: any, options?: IVerifyOptions | string) => void): void;
}
declare class Strategy implements passport.Strategy {
constructor(verify: VerifyFunction);
constructor(options: IStrategyOptions, verify: VerifyFunction);
constructor(options: IStrategyOptions, verify: VerifyFunctionWithRequest);
name: string;
authenticate(req: express.Request, options?: Object): void;
}
My gut feeling is that the constructor(options: IStrategyOptions, verify: VerifyFunctionWithRequest);
overload isn’t used as I don't see how it would know passReqToCallback
is true
. Is this a bug with the type definitions?
Upvotes: 2
Views: 663
Reputation: 249586
You are right about the overloads causing issues. My guess is that inference picks the first overload that could be valid for contextual types for parameters, but then it finds that the function has too many parameters. Reordering the parameters will not fix this either as the version with VerifyFunction
will then itself become un-accessible.
Unfortunately the workarounds are not great, you can either specify the parameters explicitly, you could use Parameters
to avoid typing out all the parameters:
passport.use(
"users",
new Strategy({ passReqToCallback: true }, (...[req, token, done]: Parameters<VerifyFunctionWithRequest>) => {
if (token !== "foo") {
return done(null, false)
}
return done(null, {}, { scope: "user" })
})
);
You could also use a type assertion but that might lead mistaken return values.
Upvotes: 3