2016-09-23 3 views
0

Ich glaube nicht, dass dies möglich ist, aber da dies:Zugriff auf eine Taste auf einem Typoskript Schnittstelle

Schnittstelle

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

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

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

Klasse:

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. 

    } 
} 

Wie Sie umgehen dieses Problem. Ich kann keine Online-Beispiele finden, die sich mit einem solchen Problem befassen. Ich gebe zu, ich bin sehr neu in all dem.

Antwort

1

Wir verwendeten Sachen Re-Export. Also, im Inneren des "./interface" können wir tun (überprüfen Sie die letzten Zeilen):

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

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

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

Und jetzt können wir importieren auch diesen Typ

// 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

Eigentlich ist das die richtige Antwort. Als ich @ AlexGs Ansatz der Verwendung von typeof versuchte, beklagte sich Typescript immer noch, dass es die "IPrimaryNavigationOpts" -Schnittstelle nicht finden konnte – Simon

0

Sie geben kann als: browserService: typeof IPrimaryNavigationOpts.browserService

+0

'Fehler TS2304: Der Name 'IPrimaryNavigationOpts' kann nicht gefunden werden. Das ist ziemlich seltsam. – Simon

Verwandte Themen