简体   繁体   English

打字稿从打字扩展类

[英]Typescript extend class from typings

I have a library which I'm building with grunt. 我有一个正在兴建的图书馆。 There's a class named SearchResult which is propertly exported in my 'd.ts' file: 有一个名为SearchResult的类已正确导出到我的d.ts文件中:

declare module my.module {
    export class SearchResult {
        id?: string;
        object_type: string;
        item?: any;
        type?: string;
    }
}

And it's compiled to this: 它被编译为:

357:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SearchResult = (function () {
    function SearchResult() {
    }
    return SearchResult;
}());
exports.SearchResult = SearchResult;
},{}]

But when I import this file using reference and trying to extend this class like that: 但是,当我使用引用导入此文件并尝试扩展此类时,如下所示:

export class SearchResultChecked extends my.module.SearchResult {
    checked?: boolean;
}

I've got a runtime error: 我遇到了运行时错误:

Uncaught TypeError: Object prototype may only be an Object or null: undefined
    at setPrototypeOf (<anonymous>)
    at __extends (_prelude.js:1)

There's many similar problems, but they're not the same. 有许多类似的问题,但它们并不相同。

tsc -v : 3.0.1 tsc -v :3.0.1

UPD : it lloks like I can't extend class which is not last defined in my library. UPD :就像我无法extend库中未最后定义的类一样。 Looking for solutions. 寻找解决方案。

.d.ts files are not emitted, they don't produce any code, and only exist on the type level (conceptually, at the linker level). .d.ts文件不会发出,它们不产生任何代码,仅存在于类型级别(概念上,在链接器级别)。

Code written in .d.ts files never makes it to runtime, it just tells TypeScript "assume that this class/enum/whatever is there, even though you aren't going to emit JS code about it" .d.ts文件编写的代码永远不会进入运行时,它只是告诉TypeScript“即使您不打算发出有关此类的JS代码,也要假定此类/枚举/存在于其中”

If you want a class to actually exist in runtime, you will need to actually export it from an actual .ts file, or import a library that exposes the class. 如果要让类在运行时中实际存在,则需要从实际的.ts文件中实际导出它,或导入一个公开该类的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM