最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to create custom datatype in typescript? - Stack Overflow

programmeradmin0浏览0评论

I have a object like below

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'
    }
};

How to define datatype like object which has array of object on that each object will be string,string

I have a object like below

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'
    }
};

How to define datatype like object which has array of object on that each object will be string,string

Share Improve this question asked Sep 28, 2018 at 2:03 ned5ned5 851 gold badge2 silver badges7 bronze badges 2
  • I'm not sure what exactly you're looking for, but this question i think is relevant: stackoverflow.com/questions/13212521/typescript-static-classes – blueberryfields Commented Sep 28, 2018 at 2:20
  • You should give a example of your array result! – protoproto Commented Sep 28, 2018 at 2:23
Add a comment  | 

1 Answer 1

Reset to default 17

It looks like you have not an array, but an object with a variable set of properties all of the same type. That can be described with a string index signature.

type AppErrorTemplateType = {
    [name: string]: {
        template: string,
        code: string
    }
};
发布评论

评论列表(0)

  1. 暂无评论