2013-05-16 85 views
3

我有一個JSON對象從$ .ajax調用返回給我。我讓我的對象從我的反應就像這樣:屬性'someProp'在類型'object'的值中不存在Typescript

var parsedJSON = $.parseJSON(jqXHR.responseText); 

,但未通過服務器傳遞的對象本身具有ResponseStatus屬性,在ResponseStatus財產,有一個錯誤代碼和消息。我覺得還是能夠做到這一點:

var r = parsedJSON.ResponseStatus; 

但我得到的錯誤:The property 'ResponseStatus' does not exist on value of type 'object'

因爲這是打字稿,當我嘗試保存文件,然後創建VS我的javascript,它不會。

我在這裏錯過了一些超級明顯的東西嗎?我可以創建一個接口,並把對象像這樣的接口:

var parsedJSON: IHttpResponseStatus = <IHttpResponseStatus> $.parseJSON(jqXHR.responseText); 
var r = parsedJSON.ResponseStatus; 

但這似乎有點小題大做,以獲取屬性,和漂亮的錯。提前致謝。

編輯:哦,從不知道,你可以使用數組符號,TS不關心。哎呦!

+0

如果更新至最新的jquery.d .ts絕對鍵入你將不再需要做投射,因爲我的拉動請求被合併https://github.com/borisyankov/DefinitelyTyped/pull/554 – basarat

回答

3

這是監守jquery.d.ts parseJSON定義爲:

parseJSON(json: string): Object; 

它應該有什麼。你可以把結果自己避免創建接口

var parsedJSON = <any> $.parseJSON(jqXHR.responseText); 
var r = parsedJSON.ResponseStatus; 

或:

var parsedJSON:any = $.parseJSON(jqXHR.responseText); 
var r = parsedJSON.ResponseStatus; 

我發送拉入請求,以及:https://github.com/borisyankov/DefinitelyTyped/pull/554

相關問題