2014-04-23 54 views
1

我突然有一個非常奇怪的編譯我的一個文件。typescript編譯器忽略定義/導入1個文件

comm.ts:

import document = require('./document/document'); 
import element = require('./document/elements/element'); 
import paragraph = require('./document/elements/paragraph'); 
import listBody = require('./document/list-body'); 
// ... 

comm.js:

var __extends = this.__extends || function (d, b) { 
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 
    function __() { this.constructor = d; } 
    __.prototype = b.prototype; 
    d.prototype = new __(); 
}; 
define(["require", "exports"], function(require, exports) { 
// ... 

什麼是與延伸,更重要的是,爲什麼定義不能在4個進口拉?它們用於代碼中。

而最大的問題是,現在當我跑我的錯誤:

SCRIPT5007:無法獲取屬性「原型」的未定義或空引用 comm.js,行18個字符5

在上面列出的代碼中。

回答

1

What's with the extends

TypeScript生成它來協助繼承。即。 class A extends B將導致添加擴展功能。

why does the define not pull in the 4 imports? They are used in the code.

我不相信它們用在代碼中。確保您有類似:

import listBody = require('./document/list-body'); 
var foo = listBody; // this will ensure code gen. 

你的錯誤`無法獲取未定義或空引用comm.js的特性「原型」是反映了這一事實。您試圖擴展未加載的東西(在生成的定義中缺少),因爲TypeScript認爲它在運行時不需要。

本地測試 我已驗證通過打字稿生成的代碼是正確的,如果它注意到了extend

foo.ts:

class Foo{} 
export = Foo; 

bar.ts:

import Foo = require('./foo'); 
class Bar extends Foo{} 

編譯: tsc --module amd bar.ts

生成bar.js

var __extends = this.__extends || function (d, b) { 
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 
    function __() { this.constructor = d; } 
    __.prototype = b.prototype; 
    d.prototype = new __(); 
}; 
define(["require", "exports", './foo'], function(require, exports, Foo) { 
    var Bar = (function (_super) { 
     __extends(Bar, _super); 
     function Bar() { 
      _super.apply(this, arguments); 
     } 
     return Bar; 
    })(Foo); 
}); 
+0

導入是必需的,但僅用於定義類型。所以我猜測對於類型定義來說,這是編譯時需要的,但不是運行時。但是,__extends函數仍然會引發異常。在入口b上是未定義的,而d是「函數RunProperties」,這是我擁有的一個類,但它不在任何地方引用此文件。 –

+0

如何找出延伸問題是什麼?我仔細查看了代碼,並且此文件中的所有擴展都是在同一個文件中的類和接口。 –

+0

是否允許循環導入?我有一個案例,file1導入file2和file2導入file1。 –

0

在我的情況,那是因爲我有一個class Call延伸的class Task,和兩個類文件在同一個文件夾中。這本身不是問題,但Typescript/gulp按照文件創建(修改?)的順序在同一文件夾中編譯文件。所以public class Call extends Task在文件Call.ts正在編譯之前class Task文件Task.ts,所以Task沒有在需要的時候被定義。因此,錯誤「無法獲取未定義或空引用的屬性'原型'」。

有點令人發狂的是,這兩個類別都有一個父類別,其中有一個祖父類別,而運算錯誤是在祖父母類別上引發的。祖父母從另一個模塊中的另一個文件夾中,並在應用程序中的許多地方成功使用。