2017-03-27 22 views
0

命名空间,我喜欢写https://github.com/LionC/express-basic-auth使用自定义类型在出口的功能

一个index.d.ts文件,但我莫名其妙地堵在了如何在选项对象申报类型回调。

declare module "express-basic-auth" { 


    import {Handler, Request} from "express"; 

    function ExpressBasicAuthorizer(username: string, password: string): boolean; 
    function ExpressBasicAuthResponseHandler(req: Request): string|object; 

    interface ExpressBasicAuthUsers { 
    [username: string]: string; 
    } 

    interface ExpressBasicAuthOptions { 
    challenge?: boolean; 
    users?: ExpressBasicAuthUsers; // does not only allow string:string but other ex. string: number too 
    authorizer?: ExpressBasicAuthorizer; // *does not work* 
    authorizeAsync?: boolean; 
    unauthorizedResponse?: ExpressBasicAuthResponseHandler|string|object; // *does not work* 
    realm?: ExpressBasicAuthResponseHandler|string; // *does not work* 
    } 

    function expressBasicAuth(options?:ExpressBasicAuthOptions): Handler; 

    export = expressBasicAuth; 

} 

我得到:错误TS2304:找不到名称 'ExpressBasicAuthorizer'

我如何可以声明ExpressBasicAuthorizer和ExpressBasicAuthResponseHandler使其作品?

回答

0

在这种情况下,ExpressBasicAuthorizerExpressBasicAuthResponseHandler需要声明为“类型”而不是“函数”。试试这个:

declare module "express-basic-auth" { 
    import { Handler, Request } from "express"; 

    type ExpressBasicAuthorizer = (username: string, password: string) => boolean; 
    type ExpressBasicAuthResponseHandler = (req: Request) => string | object; 

    interface ExpressBasicAuthUsers { 
     [username: string]: string | number; 
    } 

    interface ExpressBasicAuthOptions { 
     challenge?: boolean; 
     users?: ExpressBasicAuthUsers; 
     authorizer?: ExpressBasicAuthorizer; 
     authorizeAsync?: boolean; 
     unauthorizedResponse?: ExpressBasicAuthResponseHandler | string | object; 
     realm?: ExpressBasicAuthResponseHandler | string; 
    } 

    function expressBasicAuth(options?: ExpressBasicAuthOptions): Handler; 

    export = expressBasicAuth; 
}