2016-09-23 12 views
0

上のキー:アクセス私はこれが可能だと思うが、これを与えられていないtypescriptですインターフェイス

インタフェース

import {BrowserService} from "../services/browser/index"; 

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

export interface IPrimaryNavigationOpts { 
    ... 
    browserService:BrowserService; 
    ... 
} 

クラス:どうすればよい

import {IPrimaryNavigation, IPrimaryNavigationOpts} from "./interface"; 

export class PrimaryNavigation implements IPrimaryNavigation { 
    public opts:IPrimaryNavigationOpts; 
    ... 
    mounted():void { 
     ... 
     this.listenForBootstrap(this.opts.bsNav, this.opts.browserService); 
    } 
    listenForBootstrap(menuName:string,browserService:<???>):void { 
                 ^^^ here is the problem - 
    // I want to do the equivalent of IPrimaryNavigationOpts.browserService but can't. 
    // I don't think I should have to import the IBrowserService explicitly. 

    } 
} 

あなたはこの問題を回避します。私はこのような問題に対処するオンラインの例は見つけられないようです。私はこれのすべてに非常に新しいので、ポインタが高く評価されたことを認めます。

答えて

1

。だから、我々は(最後の行を確認してください)行うことができます"./interface"の内部:

import {BrowserService} from "../services/browser/index"; 

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

export interface IPrimaryNavigationOpts { 
    ... 
    browserService:BrowserService; 
    ... 
} 
// re-export used interface 
export { BrowserService } 

をそして今、私たちもそのタイプ

// we import the re-exported type 
import {IPrimaryNavigation, IPrimaryNavigationOpts 
     BrowserService } from "./interface"; 

export class PrimaryNavigation implements IPrimaryNavigation { 
    public opts:IPrimaryNavigationOpts; 
    ... 
    mounted():void { 
     ... 
     this.listenForBootstrap(this.opts.bsNav, this.opts.browserService); 
    } 
    listenForBootstrap(menuName:string,browserService:BrowserService):void { 
    //listenForBootstrap(menuName:string,browserService:<???>):void { 
    //             ^^^ here is the problem - 
    // I want to do the equivalent of IPrimaryNavigationOpts.browserService but can't. 
    // I don't think I should have to import the IBrowserService explicitly. 

    } 
} 
+0

これは正解です。 @ AlexGがtypeofを使用する方法を試したところ、Typescriptは 'IPrimaryNavigationOpts'インターフェースを見つけることができなかった – Simon

0

としてあなたはそれを入力することができます。私たちは、再輸出のものを使用することができますbrowserService: typeof IPrimaryNavigationOpts.browserService

+0

'エラーTS2304をインポートすることができます。名前 'IPrimaryNavigationOpts'を見つけることができません。これはかなり変です。 – Simon

関連する問題