打字稿用mixins扩展:无法读取属性' prototype'未定义的

时间:2017-05-11 13:29:01

标签: typescript compilation mixins

我尝试在Typescript 1.8中使用mixins,但有时在编译之后我有这个错误:

d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
                                                               ^

TypeError: Cannot read property 'prototype' of undefined

在已编译的文件中:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, 
    new __());
};

我的.ts文件完全符合文档:

SocialFeature.ts(母班)

import IThenable = Promise.IThenable;
import LogService from "../../services/core/LogService";
import IHaveMention from "./IHaveMention";
import ExtendService from "../../services/core/ExtendService";
import Mention from "./Mention";

abstract class SocialFeature implements IHaveMention {
    protected _id: number;

    //Basic getter/setter
    get id(): number {
        return this._id;
    }
    set id(id: number) {
        this._id = id;
    }

    //IHaveMention
    _mentions: Array<Mention>;
    public resolveMentions(comment: string): IThenable<any> {
        return null;
    }

    constructor(id) {
        this._id = id;
    }

    //JSONIFY
    get json(): IThenable<any> {
        return this.toJson();
    }
    protected toJson(): IThenable<any> {
        return new Promise( (resolve, reject) => {
            //Resolve what is necessary
            var promises = [
                this.resolveMentions(this.comment)
            ];

            Promise.all(promises).then(
                (results) => {
                    var mentions = results[0];
                    var json_promises = [
                        Mention.JsonMany(mentions)
                    ];

                    Promise.all(json_promises).then(
                        (results) => {
                            var json = {
                                id          : this._id,
                                mentions    : results[0]
                            };
                            resolve(json);
                        },
                        (error_) => {
                            LogService.GetInstance().error('SocialFeatureModel::toJson', 'error_', error_);
                            reject(error_);
                        }
                    )
                },
                (error_results) => {
                    LogService.GetInstance().error('SocialFeatureModel::toJson', 'error_results', error_results);
                    reject(error_results);
                }
            )
        });
    }
    protected toJsonVote(): IThenable<any> {
        return new Promise( (resolve, reject) => {
            this.toJson().then(
                (json) => {
                    json.rating             = 0;
                    json.isLike             = null;
                    json.data               = null;
                    json.type               = null;
                    json.feature_type       = this._feature_scope;
                    resolve(json);
                },
                (error_json) => {LogService.GetInstance().error('PostModel::toJsonVote', 'error_json', error_json);
                    reject(error_json);
                }
            )
        });
    }
}
ExtendService.GetInstance().ApplyMixins(SocialFeature, [IHaveMention]);
export default SocialFeature;

IHaveMention.ts

import IThenable = Promise.IThenable;
import {Mention} from "./Mention";
import {MentionTypeEnum} from "./Mention";
export default class  IHaveMention {
    _mentions: Array<Mention>;

    resolveMentions(comment: string): IThenable<Array<Mention>> {
        return new Promise( (resolve, reject) => {
            /* private methods */
            var _matchAll = (str, regex) => {
                var res = [];
                var m;
                if (regex.global) {
                    while (m = regex.exec(str)) {
                        res.push(m[0]);
                    }
                } else {
                    if (m = regex.exec(str)) {
                        res.push(m[0]);
                    }
                }
                return res;
            };

            var _getMention = (mention: string) => {
                mention = mention.substr(2, mention.length - 2);
                var attrs: Array<string> = mention.split(':');
                return new Mention(attrs[0], Number(attrs[1]), attrs[2].substring(0, attrs[2].length - 1));
            };

            var _getMentions = (comment: string): Array<Mention> => {
                var re = /@\[(?:STARTUP|IDEA|USER|COMMUNITY|IDEA_CONTEST|CHALLENGE*?):(?:\d*):(?:[^:]*?)\]/g;
                // var re = new RegExp(str_regexp);
                var str_mentions: Array<string> = _matchAll(comment, re);
                if(str_mentions == null)
                    str_mentions = [];
                var mentions: Array<Mention> = str_mentions.map((str_mention) => { return _getMention(str_mention)});
                return mentions;
            };

            var _getExtLinks = (comment: string): Array<Mention> => {
                var re = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/g;
                var str_mentions: Array<string> = _matchAll(comment, re);
                if(str_mentions == null)
                    str_mentions = [];
                var mentions: Array<Mention> = str_mentions.map((link) => { return new Mention(MentionTypeEnum.EXTERNAL_LINK, link, link);});
                return mentions;
            };
            /* end private methods */

            //MAIN
            var mentions: Array<Mention> = _getMentions(comment);
            var links: Array<Mention> = _getExtLinks(comment);
            this._mentions = mentions.concat(links);
            resolve(this._mentions);
        });
    }
}

Mention.ts

import IThenable = Promise.IThenable;
import LogService from "../../services/core/LogService";
import ChallengeService from "../../services/challenges/ChallengeService";
import Challenge from "../challenges/Challenge";
import CommunityService from "../../services/communities/CommunityService";
import Community from "../communities/Community";
import IdeaContest from "../idea_contests/IdeaContest";
import IdeaContestService from "../../services/contests/IdeaContestService";
import ItemService from "../../services/items/ItemService";
import UserService from "../../services/users/UserService";
import ExternalLinkService from "../../services/posts/mentions/ExternalLinkService";
import User from "../User";
export class  MentionTypeEnum{
    public static USER: string          = 'USER';
    public static ITEM: string          = 'ITEM';
    public static CHALLENGE: string     = 'CHALLENGE';
    public static COMMUNITY: string     = 'COMMUNITY';
    public static IDEA_CONTEST: string  = 'IDEA_CONTEST';
    public static EXTERNAL_LINK: string = 'EXTERNAL_LINK';
}

export class Mention {
    private _type: string;
    private _object_id: number;
    private _label: string;

    get label(): string {
        return this._label;
    }
    set label(label: string) {
        this._label = label;
    }
    get object_id(): any {
        return this._object_id;
    }
    set object_id(object_id: any) {
        this._object_id = object_id;
    }
    get type(): string {
        return this._type;
    }
    set type(type: string) {
        this._type = type;
    }
    constructor(type, object_id, label) {
        this._type          = type;
        this._object_id     = object_id;
        this._label         = label;
    }

    get json(): IThenable<any> {
        return new Promise( (resolve, reject) => {
            this._getOneInformations().then(
                (informations) => {
                    var json = {
                        type: this._type,
                        object_id: this._object_id,
                        label: this._label,
                        data: informations
                    };
                    resolve(json);
                },
                (error_information) => {
                    LogService.GetInstance().error('Mention::json', 'error_information', error_information);
                    reject(error_information);
                }
            )
        });
    }

    public static JsonMany(mentions: Array<Mention>): IThenable<Array<any>> {
        return new Promise( (resolve, reject) => {
            var promises = mentions.map((mention) => {return mention.json});
            resolve(Promise.all(promises));
        });
    }

    private _getOneInformations(): IThenable<any> {
        return new Promise( (resolve, reject) => {
            var mention = this;
            switch (mention.type) {
                case MentionTypeEnum.CHALLENGE:
                    var promise: IThenable<any> = <IThenable<any>> ChallengeService.GetInstance().GetById(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        object = <Challenge> object;
                        return Challenge.toJSON(object)
                    };
                    break;
                case MentionTypeEnum.COMMUNITY:
                    var promise: IThenable<any> = <IThenable<any>> CommunityService.GetInstance().GetById(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        object = <Community> object;
                        return object.jsonDescription
                    };
                    break;
                case MentionTypeEnum.IDEA_CONTEST:
                    var promise: IThenable<any> = <IThenable<any>> IdeaContestService.GetInstance().GetById(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        object = <IdeaContest> object;
                        return object.jsonDescription
                    };
                    break;
                case MentionTypeEnum.ITEM:
                    var promise: IThenable<any> = <IThenable<any>> ItemService.GetInstance().GetItemById(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        return object
                    };
                    break;
                case MentionTypeEnum.USER:
                    var promise: IThenable<any> = <IThenable<any>> UserService.GetInstance().GetUserById(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        object = <User> object;
                        return object.jsonResume
                    };
                    break;
                case MentionTypeEnum.EXTERNAL_LINK:
                    var promise: IThenable<any> = <IThenable<any>> ExternalLinkService.GetInstance().GetInfos(mention.object_id);
                    var promiseJson = (object: any): IThenable<any> => {
                        return object
                    };
                    break;
                default:
                    resolve({});
                    break;
            }

            promise.then(
                (object) => {
                    resolve(promiseJson(object));
                },
                (error_object) => {
                    LogService.GetInstance().error('MentionService::GetOneInformations', 'error_object', error_object);
                    reject(error_object);
                }
            )
        });
    }
}

我的tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "target": "ES5",
        "sourceMap": true,
        "outDir": "./build"
    },
    "filesGlob": [
        "./sources/*/**.ts"
    ]
}

我尝试使用&#34; ///&#34;引用我的文件使用而不是母类的表示法,因为我认为这是编译期间的订单问题,但没有任何变化..

你有什么想法吗?或者你能解释一下如何实现吗?

编辑2: 经过硬调试后,我在javascript文件中发现SocialFeature对象永远不会被导出..所以,在CommunityEvaluation中SocialFeature_1.default是未定义的.. 但我无法解释为什么......

0 个答案:

没有答案