2014-04-23 49 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没有在需要的时候被定义。因此,错误“无法获取未定义或空引用的属性'原型'”。

有点令人发狂的是,这两个类别都有一个父类别,其中有一个祖父类别,而运算错误是在祖父母类别上引发的。祖父母从另一个模块中的另一个文件夹中,并在应用程序中的许多地方成功使用。