以下代码:
enum Type { digit=1, alpha=2, alnum=3 }
enum Transform{uppercase=1,lowercase}
interface Options {
type: Type;
length: number;
min: number;
max: number;
uppercase: boolean;
lowercase: boolean;
transform: Transform;
valueChange: (element:Object,value:string) => string;
}
class InputFilter {
constructor(private options: Options, private element: Object) {
}
}
我想让options
不仅拥有界面,还拥有默认值,就像这样:
options = {
"type": "alnum",
"length": null,
"min": 0,
"max": Infinity,
"uppercase": true,
"lowercase": true,
"transform": null,
"valueChange": function(element, value) {}
};
我该怎么做?
答案 0 :(得分:2)
您需要在某处创建工厂方法或函数,以创建一个符合您的界面的默认值的对象。
以下是一个例子:
function createOptionsWithDefaultValues(): Options {
return {
type: Type.alnum,
length: null,
min: 0,
max: Infinity,
uppercase: true,
lowercase: true,
transform: null,
valueChange: function(element, value) {
return null;
}
};
}
let options = createOptionsWithDefaultValues();
// use options...