2017-08-07 78 views
0

我正在將大量Javascript文件轉換爲項目中的Typescript文件。目前,我一直在努力轉換Range3.js,一個依賴於另一個名爲Cartesian3.js的Javascript文件的文件,我爲Cartesian3.js編寫了一個定義文件(d.ts),並且項目編譯了,但是在運行時,我得到這個錯誤:擴展原型的定義文件

Uncaught ReferenceError: Cartesian3 is not defined 

Cartesian3.js的格式是這樣的:

define([ 
    './TypescriptFile' 
    ], 
function(
    TypescriptFile 
    ) 
{ 
    /* file has some prototype-extending functions like this */ 
    Cesium.Cartesian3.prototype.extendingFunction = function() 
    { 
     /* function defined here */ 
    } 

    /* and some static functions like this */ 
    Cesium.Cartesian.staticFunction = function() 
    { 
     /* function defined here */ 
    } 

    /* more functions... */ 

    return Cesium.Cartesian3; 
}); 

的d.ts文件我寫了Cartesian3.js看起來是這樣的:

///<reference path="../../Cesium/Cesium.d.ts"/> 
declare class Cartesian3 extends Cesium.Cartesian3 { 
    extendingFunction(): void; 
    static staticFunction(): Cartesian3; 
    /* more functions listed here */ 
} 

Range3.ts (文件t帽子拋出的ReferenceError)看起來是這樣的:

///<reference path="./Cartesian3.d.ts"/> 
/* another import */ 

class Range3 
{ 
    low: Cartesian3; 
    /* more instance variables */ 

    /* truncated constructor */ 
    constructor(low?: Cartesian3) 
    { 
     this.low = Cesium.defined(low) ? <Cartesian3>low.clone() : new Cartesian3; // line that throws an error 
    } 

    /* more functions */ 
} 

Range3.js(與項目工程)是這樣的:

define([ 
    './Cartesian3', 
    /*another dependency*/ 
    ], 
function(
    Cartesian3, 
    /*other dependency */ 
    ) 
{ 
    function Range3(low) 
    { 
     this.low = Cesium.defined(low) ? low.clone() : new Cartesian3(); 
    } 
    /* other functions defined here */ 
    return Range3; 
}); 

我懷疑這是由於不正確執行定義文件但我無法確定問題。任何幫助表示讚賞!

+0

如果錯誤是在運行時,我不認爲定義文件會影響它。你是否包含所有合適的.js文件? – libertyernie

+0

是的,Cartesian3.js包含在內,Cesium的庫也是如此 – anon

回答

0

這不完全是一個解決方案,但我找到了解決方法。由於Javascript文件擴展了Cesium對象(Cartesian3)的原型,因此我在Cesium的des文件中爲Cartesian3添加了額外的函數,並刪除了Cartesian3.d.ts,而在Range3.ts中引用了Cesium.d.ts,修復了錯誤。如果有人願意找到更好的解決方案,我很想知道。