2017-04-21 81 views
2

我正在为一组应用程序特定的API创建一些智能感知,并且好奇它是否可以将回调函数的参数定义为先前定义的接口?我担心我甚至不能正确地问这个问题,所以我只会告诉你代码。是否可以将回调函数参数定义为命名空间接口?

我有一个包含打字稿定义的intellisense的绝对类型的文件。它相当长,所以我只会显示相关的内容。

declare interface N_search { 
    create: { 
     (options: { 
      type: string 
     }): N_search.Search 
} 

declare namespace N_search { 

    interface Search { 
     run(): N_search.ResultSet 
    } 
    interface Result { 
     type: string 
     id: number 
    } 
    interface ResultSet { 
     each: { 
      (callback (N_search.Result)) : void //aware this is not correct... 
    } 

可能很难从那个混乱中收集,但ResultSet接口内部的(回调)是一个函数。该函数的参数是一个N_search.Result对象,我希望intellisense显示这一点。这是我期待获得智能感知的JavaScript。

var search = N_search.create(options); 

search.run().each(function (result) { 
    result. /* I want intellisense here to show the N_search.Result object 
     which should be type: string ; id: number */ 
}); 

我希望我的问题有道理,我非常感谢任何帮助!

+0

不'回调=()=> N_search.Result'不行? – Daryl

回答

2

定义您ResultSet接口,如:

interface ResultSet { 
    each: (callback: (result: N_search.Result) => void) => void   
} 
+0

我很尴尬地说,我花了多少时间来解决这个问题!你先生已经确保我今晚能入睡:) –

+0

我们都在那里:)没有什么好尴尬的。 – Saravana

相关问题