Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:UnitType不是构造函数_Javascript_Arrays_Typescript - Fatal编程技术网

Javascript 未捕获类型错误:UnitType不是构造函数

Javascript 未捕获类型错误:UnitType不是构造函数,javascript,arrays,typescript,Javascript,Arrays,Typescript,我有一个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',

我有一个
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'),
];
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}';

}}
当我编译代码时,我得到

未捕获类型错误:UnitType不是构造函数

为什么会出现此错误,如何解决?
我正在运行TypeScript 1.8.1。

我想您可能会遇到由于不正确或缺少依赖项而导致传输顺序错误的问题。我不知道你是如何在文件之间分发你的东西的,但是下面的布局对我很有用。当然,您可能需要根据您的环境相应地调整文件名和导入/引用。
希望有帮助

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'),
]; 
export type MeasurementType = 'weight' | 'length' | 'temperature' | 'pressure';
export type UnitKeyType =
    'kg' | 'lb'
    | 'cm' | 'm' | 'ft' | 'in';

这非常有效。尽管您指定了import语句,但为什么///是必需的?您是对的,这不是必需的-它在另一个(旧)文件中,我用它作为模板,但没有删除它-很好!我只是好奇。我还以为这是另外一件我不熟悉的事,因为我是打字新手。