透明化似乎将方法分开了

时间:2017-11-28 18:14:02

标签: typescript ecmascript-6 tsconfig

我遇到一个奇怪的问题,即转换器似乎将一个方法分成两部分:

在发布之前:

MemberSchema.methods.addAccount = (account: IAccount): void => {
    this.account = new Account({
        ...account,
        isActive: true
    });

    bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
        if(err) throw err;

        bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
            if(err) throw err;

            this.account.password = hash;
        });
    });
};

发布后:

exports.MemberSchema.methods.addAccount = (account) => {
    this.account = new account_1.Account({}, ...account, isActive, true);
};
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
    if (err)
        throw err;
    bcrypt.hash(this.account.password, salt, null, function (err, hash) {
        if (err)
            throw err;
        this.account.password = hash;
    });
});

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES6",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "types": ["reflect-metadata"],
    "lib": ["ES6"],
    "sourceMap": true,
    "inlineSources": true,
    "pretty": true,
    "outDir": "dist",
    "rootDir": "src",
    "noLib": false,
    "declaration": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

导致此问题的原因是什么?是否有需要更改的配置才能解决此问题,或者是否需要更改typescript文件?

更新

Member.ts:

import { Schema, Model } from "mongoose";
import mongoose = require("mongoose");
import {Account, AccountSchema} from "../Account/account";
import {IAccount} from "../Account/iaccount";
import {isNullOrUndefined} from "util";
import {IMemberModel} from "./imembermodel";

export let MemberSchema: Schema = new Schema({
    account: AccountSchema,
    permissions: [{type: Schema.Types.String, enum: ["regularuser", "admin", "owner"]}],
    isActive: Schema.Types.Boolean
});
MemberSchema.pre("save", next => {
    this.isActive = true;

    next();
    return this;
});
MemberSchema.methods.addAccount = (account: IAccount): void => {
    ***this.account = new Account({
        ...account,
        registrationComplete: true
    });***

    bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
        if(err) throw err;

        bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
            if(err) throw err;

            this.account.password = hash;
        });
    });
};
MemberSchema.methods.updateAccount = (account: IAccount): void => {
    if (!isNullOrUndefined(account.password)){
        bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
            if(err) throw err;

            bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
                if(err) throw err;

                this.account.password = hash;
            });
        });
    }
};

export const Member: Model<IMemberModel> = mongoose.model<IMemberModel>("Member", MemberSchema);

Member.js:

"use strict";
const mongoose_1 = require("mongoose");
const mongoose = require("mongoose");
const account_1 = require("../Account/account");
const util_1 = require("util");
exports.MemberSchema = new mongoose_1.Schema({
    account: account_1.AccountSchema,
    permissions: [{ type: mongoose_1.Schema.Types.String, enum: ["regularuser", "admin", "owner"] }],
    isActive: mongoose_1.Schema.Types.Boolean
});
exports.MemberSchema.pre("save", next => {
    this.isActive = true;
    next();
    return this;
});
exports.MemberSchema.methods.addAccount = (account) => {
    this.account = new account_1.Account({}, ...account, registrationComplete, true);
};
;
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
    if (err)
        throw err;
    bcrypt.hash(this.account.password, salt, null, function (err, hash) {
        if (err)
            throw err;
        this.account.password = hash;
    });
});
;
exports.MemberSchema.methods.updateAccount = (account) => {
    if (!util_1.isNullOrUndefined(account.password)) {
        bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
            if (err)
                throw err;
            bcrypt.hash(this.account.password, salt, null, function (err, hash) {
                if (err)
                    throw err;
                this.account.password = hash;
            });
        });
    }
};
exports.Member = mongoose.model("Member", exports.MemberSchema);
//# sourceMappingURL=member.js.map

更新2: 传播运营商似乎是问题所在。

当我注释掉这一行时:

this.account = new Account({
            **//...account,**
            registrationComplete: true
        });

似乎工作正常。如果扩展运算符是常规的es6语法,为什么会出现如此重大的转换问题呢?

1 个答案:

答案 0 :(得分:1)

您尝试使用不支持对象传播运算符的旧版TypeScript进行编译(运行tsc -v将显示2.0或更早版本)。将tsc升级到2.1或更高版本。

WebStorm也可能使用旧版本的TypeScript进行编译保存;阅读他们的文档,了解如何升级用于转换文件的TS版本。