Axel Dolce
Axel Dolce

Reputation: 11

Visual Code d.ts define Promise resolve type

having:

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

i want VSCode to suggest the following

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

is this possible?

Upvotes: 1

Views: 204

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141632

If I understand correctly, then this will meet your requirement.

// 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;
});

Here is an image of the suggestion in the TypeScript playground:

enter image description here

Upvotes: 1

Related Questions