我正在为一组特定于应用程序的 API 创建一些智能感知,并且很好奇是否可以将回调函数的参数定义为先前定义的接口(interface)?我担心我什至无法正确地提出这个问题,所以我只会向您展示代码。
我有一个definitelyTyped 文件,其中包含我的 typescript 定义的智能感知。它很长,所以我只会显示相关的内容。
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...
}
可能很难从困惑中收集起来,但接口(interface) ResultSet 内部的(回调)是一个函数。该函数的参数是 N_search.Result 对象,我希望智能感知显示这一点。这是我想要获取智能感知的 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 */
});
我希望我的问题有意义,非常感谢您的帮助!
最佳答案
定义您的 ResultSet
接口(interface),例如:
interface ResultSet {
each: (callback: (result: N_search.Result) => void) => void
}
关于javascript - 是否可以将回调函数参数定义为命名空间接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532647/