如何在打字稿中创建自定义数据类型?

ned*_*ed5 5 javascript types typescript

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

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'
    }
};
Run Code Online (Sandbox Code Playgroud)

如何定义具有对象数组的对象的数据类型,每个对象将是字符串,字符串

Mat*_*hen 16

看起来您没有一个数组,而是一个具有所有相同类型的可变属性集的对象。这可以用字符串索引签名来描述。

type AppErrorTemplateType = {
    [name: string]: {
        template: string,
        code: string
    }
};
Run Code Online (Sandbox Code Playgroud)