typescript 定义对象

像这样的对象应该怎么定义及写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这个是可变的字符串,而它内部的结构是一定的,但是不知道怎么写,
求帮助……data:what中的interface what该怎么写

阅读 8.1k
3 个回答
interface what{
    [index:string]:{
        defaultValue:string,
        allowNull:boolean,
        set:{():void},
        get:{():void},
        validate:{
            [index:string]:{
                args:[]any,
                msg:string
            }
        }
    }
}

就是username和passport这个字段有可能有,有可能没有?

interface Wechat {
    username?:Validate;
    password?:Validate;
}
interface Validate {
        defaultValue:string,
        allowNull:false,
        get:Function,
        set:Function,
        validate:{[index:string]:any}
        }
}
interface IWhatSubStruct {
    defaultValue: string,
    allowNull: boolean,
    get: () => void,
    set: () => void,
    validate: {
        len: {
            args: Array<number>,
            msg: string
        }
    }
}

interface what {
    username?: IWhatSubStruct;
    password?: IWhatSubStruct;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进