2017-07-31 151 views
1

我有一個擴展EventEmmiter類的類。找不到TypeScript聲明類

我想智能感知顯示,你可以聽不同的事件,並幫助汽車完成它們,所以我想出了這個:

//Imports from index.ts 
import { 
    CommandContext, 
    CommandErrorContext, 
    CommandHandler 
} from '../../'; 
import { EventEmitter } from 'events'; 

export class CommandHandler extends CommandHandlerEvents { 

    constructor() { 
     super(); 
    } 

} 

export declare class CommandHandlerEvents extends EventEmitter { 
    on(event: string, listener: Function): this; 
    on(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this; 
    on(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this; 

    once(event: string, listener: Function): this; 
    once(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this; 
    once(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this; 

    emit(event: string, args: any[]): boolean; 
    emit(event: 'failure', args: [CommandHandler, CommandErrorContext]): boolean; 
    emit(event: 'success', args: [CommandHandler, CommandContext]): boolean; 
} 

上面的代碼不正是我想要它做的,但是當我創建CommandHandler類的實例,它拋出這個錯誤:

Uncaught ReferenceError: CommandHandlerEvents is not defined

我看了一下使用接口做同樣的事情的人,但是當我改變CommandHandlerEventsdeclare classinterface這些建議不再顯示在IntelliSense上。理想的結果的

例子:image

+0

是否在任何地方聲明瞭「EventEmitter」或「CommandErrorContext」或「CommandContext」? – recursive

+0

@recursive是的,我會更新代碼 – DusterTheFirst

回答

1

當你declare一個class,或使用interface,你只是在編譯時添加東西的類型系統。您不會在運行時上添加任何內容。如果你看,沒有任何名字CommandHandlerEvents被髮射到JavaScript。 declare關鍵字告訴編譯器,你正在說的東西將在運行時存在,因爲它將由任何運行時環境運行代碼提供。

您是否想要EventEmitter的所有用途具體說明您正在談論的內容?如果是這樣,你可以module augmentation添加這種類型的信息:

import { EventEmitter } from 'events'; 
declare module 'events' { 
    interface EventEmitter { 
    on(event: string, listener: Function): this; 
    on(event: 'failure', listener: (handler: EventEmitter, context: CommandErrorContext) => void): this; 
    on(event: 'success', listener: (handler: EventEmitter, context: CommandContext) => void): this; 

    once(event: string, listener: Function): this; 
    once(event: 'failure', listener: (handler: EventEmitter, context: CommandErrorContext) => void): this; 
    once(event: 'success', listener: (handler: EventEmitter, context: CommandContext) => void): this; 

    emit(event: string, args: any[]): boolean; 
    emit(event: 'failure', args: [EventEmitter, CommandErrorContext]): boolean; 
    emit(event: 'success', args: [EventEmitter, CommandContext]): boolean; 
    } 
} 

const eventEmitter = new EventEmitter(); 
eventEmitter.once(...) // get hints 

或者你真的想在運行時類CommandHandlerEventEmitter更具體?你也可以用declaration merging做到這一點:

import { EventEmitter } from 'events'; 
export class CommandHandler extends EventEmitter { } 
export interface CommandHandler { 
    on(event: string, listener: Function): this; 
    on(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this; 
    on(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this; 

    once(event: string, listener: Function): this; 
    once(event: 'failure', listener: (handler: CommandHandler, context: CommandErrorContext) => void): this; 
    once(event: 'success', listener: (handler: CommandHandler, context: CommandContext) => void): this; 

    emit(event: string, args: any[]): boolean; 
    emit(event: 'failure', args: [CommandHandler, CommandErrorContext]): boolean; 
    emit(event: 'success', args: [CommandHandler, CommandContext]): boolean; 
} 

const commandHandler = new CommandHandler(); 
commandHandler.once(...) // get hints 

希望有所幫助!

+0

謝謝你的詳細解釋,這是非常有用的,並解決了我的問題。 我最終使用減速合併來實現我的目標。 – DusterTheFirst