【问题标题】:how to create custom datatype in typescript?如何在打字稿中创建自定义数据类型?
【发布时间】:2019-03-03 23:27:18
【问题描述】:

我有一个像下面这样的对象

export const appErrorTemplate = {
    NO_APP : {
        template : 'Unable to initialize #{0}()!',
        code : 'no-app'
    },
    NO_REQ_FOUND : {
        template : '#{0}() requires \'#{1}\' to process!',
        code : 'no-required-found'
    },
    MISMATH : {
        template : '#{0}() required #{1} but \'#{2}\' found!',
        code : 'mismatch'
    },
    NOT_SATISFY : {
        template : 'Given parameter on #{0}() does not satisfied #{1} constrains',
        code : 'not-satisfy'
    },
    UNKNOWN : {
        template : 'Something went wrong!',
        code : 'unknown'
    }
};

如何定义像对象这样的数据类型,它有一个对象数组,每个对象都是字符串,字符串

【问题讨论】:

标签: javascript typescript types


【解决方案1】:

看起来你没有一个数组,而是一个具有一组可变属性的对象,所有这些属性都是相同的类型。这可以用字符串index signature来描述。

type AppErrorTemplateType = {
    [name: string]: {
        template: string,
        code: string
    }
};

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2020-08-02
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多