Come faccio a refactoring annotazioni di tipo?

voti
1

Supponiamo che sto definendo un modulo, e ho alcune definizioni di funzioni come questo:

export function bodyParser(options?:any): 
  (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void;
export function errorHandler(opts?:any): 
  (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void;
export function methodOverride(): 
  (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void;
export function favicon(url: string, opts? ): 
  (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void;
export function logger(type: string, opts? ): 
  (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void;

Sto più volte di utilizzare questo tipo di ritorno della nota:

(req: ExpressServerRequest, res: ExpressServerResponse, next) =>void

C'è un modo per definire quel tipo, assegnare un nome, e basta fare riferimento al nome invece di ripetere la stessa cosa?

È pubblicato 04/10/2012 alle 23:51
fonte dall'utente
In altre lingue...                            


1 risposte

voti
6

Sì! È possibile utilizzare l'interfaccia con una firma di chiamata:

interface MyCallSignature {
    (req: ExpressServerRequest, res: ExpressServerResponse, next): void;
}

Ora è possibile utilizzarlo come:

export function bodyParser(options?: any): MyCallSignature
Risposto il 04/10/2012 a 23:56
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more