鍍金池/ 問(wèn)答/HTML/ typescript 定義對(duì)象

typescript 定義對(duì)象

像這樣的對(duì)象應(yīng)該怎么定義及寫interface

var data:what = {
    username:{
        defaultValue:'',
        allowNull:false,
        get:function(){},
        set:funciont(){},
        validate:{
            len:{
                args:[2,10],
                msg:'老鐵名字不合適?。?
            }
        }
    },
    password:{
        defaultValue:'',
        allowNull:false,
        get:function(){},
        set:funciont(){},
        validate:{
            len:{
                args:[2,10],
                msg:'老鐵要注意安全'
            }
        }    
    }
}

就是username和password這個(gè)是可變的字符串,而它內(nèi)部的結(jié)構(gòu)是一定的,但是不知道怎么寫,
求幫助……data:what中的interface what該怎么寫

回答
編輯回答
巷尾

就是username和passport這個(gè)字段有可能有,有可能沒(méi)有?

interface Wechat {
    username?:Validate;
    password?:Validate;
}
interface Validate {
        defaultValue:string,
        allowNull:false,
        get:Function,
        set:Function,
        validate:{[index:string]:any}
        }
}
2017年7月27日 23:04
編輯回答
墨染殤
interface IWhatSubStruct {
    defaultValue: string,
    allowNull: boolean,
    get: () => void,
    set: () => void,
    validate: {
        len: {
            args: Array<number>,
            msg: string
        }
    }
}

interface what {
    username?: IWhatSubStruct;
    password?: IWhatSubStruct;
}
2018年7月8日 15:26
編輯回答
愚念
interface what{
    [index:string]:{
        defaultValue:string,
        allowNull:boolean,
        set:{():void},
        get:{():void},
        validate:{
            [index:string]:{
                args:[]any,
                msg:string
            }
        }
    }
}
2017年6月4日 19:57