【问题标题】:Visual Code d.ts define Promise resolve typeVisual Code d.ts 定义 Promise 解析类型
【发布时间】:2018-01-05 18:26:06
【问题描述】:

拥有:

// api.js
export function getLayout(){
    return axios.get('/api/layout').then(res => res.data)
}
// api.d.ts
declare interface JSONResponse {
    meta: object,
    data: Array<Field>
}
export declare function getLayout():Promise<any> // Promise<any>.then<JSONResponse>??

我希望 VSCode 提出以下建议

getLayout().then(json => {
    json.data // suggest data is of type Array<Fields>
})

这可能吗?

【问题讨论】:

    标签: typescript visual-studio-code typescript-typings vscode-settings


    【解决方案1】:

    如果我理解正确,那么这将满足您的要求。

    // api.d.ts
    declare interface JSONResponse {
        meta: object,
        data: Array<Fields>
    }
    
    export declare function getLayout(): Promise<JSONResponse>
    
    // client.ts
    var data = getLayout().then((json) => {
        json.data;
    });
    

    这是 TypeScript 游乐场中建议的图片:

    【讨论】:

    • 我第一次尝试过,但没有得到建议。现在工作正常。奇怪。
    • 它不起作用,因为真正的 getLayout (getLayout(name:string):Promise) 需要参数名称。通过不带名称调用它,智能感知无法正常工作(或如我所料)
    猜你喜欢
    • 2016-06-14
    • 2016-01-04
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2021-06-03
    • 2016-12-19
    • 2022-01-18
    • 2020-06-24
    相关资源
    最近更新 更多