Reputation: 580
Code below:
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) {
}
}
I wanna to make options
not only have interface,also have defaulte value,just like this:
options = {
"type": "alnum",
"length": null,
"min": 0,
"max": Infinity,
"uppercase": true,
"lowercase": true,
"transform": null,
"valueChange": function(element, value) {}
};
How can I do that?
Upvotes: 0
Views: 100
Reputation: 106650
You will need to create a factory method or function somewhere that will create an object with the default values that conforms to your interface.
Here's an example:
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...
Upvotes: 2