未捕获的TypeError: UnitType不是构造函数
Uncaught TypeError: UnitType is not a constructor
我有一个UnitType
实例数组,定义如下:
export type MeasurementType = 'weight' | 'length' | 'temperature' | 'pressure';
export type UnitKeyType =
'kg' | 'lb'
| 'cm' | 'm' | 'ft' | 'in';
export var UNIT_TYPES: Array<UnitType> = [
new UnitType('kg', 'kilogram', 'weight'),
new UnitType('lb', 'pound', 'weight'),
new UnitType('cm', 'centimeter', 'length'),
new UnitType('m', 'meter', 'length'),
new UnitType('ft', 'feet', 'length'),
new UnitType('in', 'inch', 'length'),
];
我的UnitType
类定义如下:
export class UnitType {
constructor(private _code: UnitKeyType, private _name: string,
private _measurementType: MeasurementType) {
}
get code(): string {
return this._code;
}
get name(): string {
return this._name;
}
get measurementType(): MeasurementType {
return this._measurementType;
}
static fromKey(key: UnitKeyType): UnitType {
let unit = UNIT_TYPES.filter(unit => unit.code === key)[0];
return unit;
}
toString(): string {
return this.toFormatString();
}
toFormatString(): string {
return '${this.measurementType}: ${this.name}';
}}
当我编译代码时,我得到
Uncaught TypeError: UnitType不是构造函数
为什么我得到这个错误,我如何解决它?
我猜您由于不正确或缺少依赖关系而以错误顺序编译的问题。我不知道你是如何在文件之间分配你的东西,但下面的布局对我有用。当然,您可能需要根据您的环境相应地调整文件名和导入/引用。
希望能有所帮助
UnitType.ts
import {MeasurementType, UnitKeyType} from './Util.ts'
export class UnitType {
constructor(private _code: UnitKeyType, private _name: string,
private _measurementType: MeasurementType) {
}
get code(): string {
return this._code;
}
get name(): string {
return this._name;
}
get measurementType(): MeasurementType {
return this._measurementType;
}
static fromKey(key: UnitKeyType): UnitType {
let unit = UNIT_TYPES.filter(unit => unit.code === key)[0];
return unit;
}
toString(): string {
return this.toFormatString();
}
toFormatString(): string {
return '${this.measurementType}: ${this.name}';
}}
export var UNIT_TYPES: Array<UnitType> = [
new UnitType('kg', 'kilogram', 'weight'),
new UnitType('lb', 'pound', 'weight'),
new UnitType('cm', 'centimeter', 'length'),
new UnitType('m', 'meter', 'length'),
new UnitType('ft', 'feet', 'length'),
new UnitType('in', 'inch', 'length'),
];
Util.ts
export type MeasurementType = 'weight' | 'length' | 'temperature' | 'pressure';
export type UnitKeyType =
'kg' | 'lb'
| 'cm' | 'm' | 'ft' | 'in';
相关文章:
- ES6构造函数返回基类的实例
- 使用Google Visualization动态调用构造函数
- javascript中对象构造函数中的var属性与this.properties
- 理解typescript中的构造函数接口
- 为什么构造函数不是构造函数
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 拦截对构造函数的调用
- 使用闭包共享构造函数参数
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 如何使用此从对象访问构造函数
- Javascript:为什么是构造函数's __proto__属性Empty(){}
- 当一个重要的构造函数参数丢失时应该发生什么
- Chrome Javascript日期构造函数错误
- 如何使用构造函数's的输出,以便将值插入到对象中
- 为什么在调用父构造函数时在[]中发送数据
- 构造函数函数闭包变量
- ES6类是否与构造函数相同
- 构造函数中的事件处理程序与构造函数外的事件处理函数的行为不同
- 未捕获的TypeError: UnitType不是构造函数