1
我有一個類屬性,它是不確定的,因爲它不是transpiled到JS:類屬性沒有得到transpiled
打字稿代碼:
import * as _ from "lodash"
/**
* User
*/
class User {
public properties: {
gender: string
name: string
first_name: string
last_name: string
email: string
fb_id: string
}
constructor(data) {
_.forOwn(data, (value: string, key: string) => // Needs fat arrow to bind 'this'
{
if (value) { this.properties[ key ] = value }
})
}
public useProperties() {
return this.properties
}
}
export default User
Transpiled代碼:
"use strict";
var _ = require("lodash");
var User = (function() {
function User(data) {
var _this = this;
_.forOwn(data, function (value, key) {
if (value) {
_this.properties[key] = value;
}
});
}
User.prototype.useProperties = function() {
return this.properties;
};
return User;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = User;
//# sourceMappingURL=user_model.js.map
你可以看到對象的'屬性'沒有被轉換,因此循環無法工作。爲什麼是這樣以及如何強制適當的翻譯?
謝謝,你會如何用'as'語法替換? –
@RobertBrax'= {};任何;'' –