2017-06-16 108 views
0

我有一段代碼,看起來像這樣如何標記標記函數的返回類型爲自定義類型

import * as Promise from 'bluebird'; 
import {Response} from '../commonInterfaces/httpService'; 

getCall(url: string, accessToken: string, extraHeaders: object): Promise<Response> { 
    let headers = this.getHeaders(accessToken, extraHeaders); 
    return new Promise((resolve, reject) => { 
     request({ 
      url: url, 
      method: "GET", 
      withCredentials: false, 
      json: true, 
      headers: headers 
     }, (err: Error, response: Response) => { 
      if (err) { 
       reject(err) 
      } else { 
       resolve(response) 
      } 
     }); 
    }); 
} 

和響應的承諾是一個接口

interface Response {statusCode: number, body: object} 

和我收到以下錯誤

[ts] 
Type 'Bluebird<{}>' is not assignable to type 'Bluebird<Response>'. 
Type '{}' is not assignable to type 'Response'. 
Property 'statusCode' is missing in type '{}'. 

可能是什麼問題?

回答

1

似乎編譯器不會從調用resolve(response)推斷出該類型。
您可以通過自己指定一般約束來「幫助」它:

return new Promise<Response>((resolve, reject) => { ... }); 
相關問題