访问打字稿界面上的键

时间:2016-09-23 03:37:11

标签: typescript interface

我不认为这是可能的,但鉴于此:

接口

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.

    }
}

你如何解决这个问题。我似乎无法找到任何处理此类问题的在线示例。我承认我对所有这一切都很陌生,所以指点赞赏。

2 个答案:

答案 0 :(得分: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.

    }
}

答案 1 :(得分:0)

您可以将其输入为:browserService: typeof IPrimaryNavigationOpts.browserService