2013-04-10 109 views
1

我想在一個打字稿文件中使用window.devicePixelRatio但這種失敗的消息 編譯「屬性‘devicePixelRatio’不上的值類型‘窗口’存在」打字稿window.devicePixelRatio

是有沒有修復或必須使用打字稿以外的功能?

皮特

回答

4

可以擴展Window接口與你需要的功能 - 你可以做到這一點,每當新的東西(ISH)並沒有把它做成lib.d.ts。您可能需要稍後刪除您的擴展程序,但它會將其擴展爲lib.d.ts,但編譯器當時會發出警告。

interface Window { 
    devicePixelRatio: number; 
} 

var x = 1; 

if (window.devicePixelRatio) { 
    x = window.devicePixelRatio 
} 
+0

感謝 - 這是真正有用的 – 2013-04-10 13:15:48

3

如果它適合您,我會與史蒂夫的解決方案一起去。有時候視覺工作室變得古怪,並開始抱怨變量已經定義。或者你總是可以做:

var x = 1; 
var win:any = window; 

if (win.devicePixelRatio) { 
    x = win.devicePixelRatio 
} 

var x = 1; 

if ((<any>window).devicePixelRatio) { 
    x = (<any>window).devicePixelRatio 
}