打字稿 - 用数组文字初始化所需属性时将其铸造为对象



我正在尝试初始化具有带有对象的接口类型的对象:

return {
            id: 0,
            name: name,
            localizedLabels: [
                {localeCd: defaultLocaleCd, label: name}
            ],
            localizedDescriptions: [],
            useForNetworkBuild: false,
            temporalSplitting: false,
            compounds: [],
            primaryElements: [],
            defaultMapIcon: null,
            defaultRegularIcon: null,
            markerColor: null,
            nodeShape: null
};

对象的类型是我创建的界面,称为实体。它有几个父界面。引起问题的一个是本地化对象的"局部描述"属性(对不起,深层层次结构):

export interface Entity extends hubBase.PersistableObject, 
                                hubLocalization.LocalizableObject, 
                                hubBase.GraphicallyDisplayable {
    name: string;
    useForNetworkBuild: boolean;
    temporalSplitting: boolean;
    compounds: Compound[];
    primaryElements: Element[];
}
export interface LocalizedLabel extends hubBase.PersistableObject {
    localeCd: string
    label: string;
}
export interface LocalizedDescription extends hubBase.PersistableObject {
    localeCd: string
    description: string;
}
export interface LocalizableObject {
    label?: string;
    localizedLabels: LocalizedLabel[];
    description?: string;
    localizedDescriptions: LocalizedDescription;
}

编译器给出了有关" LocalCD"属性的错误,抱怨" LentizedDescript"的[]初始化器:

我试图将整个对象施放到Entity。我还试图将[]的演员放在分配中的"本地化描述"中。错误仍然存在。有没有办法施放此属性以删除此错误?

我正在使用打字稿1.4.1。

export interface LocalizableObject {
    label?: string;
    localizedLabels: LocalizedLabel[];
    description?: string;
    // Not an array type, did you mean LocalizedDescription[] ?
    localizedDescriptions: LocalizedDescription;
}

最新更新