Define Mandantory Datatype for Arrays in Typescript
I Want to Define a jsonArray and Also the Datatypes, Which Have to Be Used. My First Approach Was This One Hear: Felder = [{ elementId: String, // elementId...
I want to define a jsonArray and also the datatypes, which have to be used.
My first approach was this one hear:
felder = [{
elementId: string, // elementId: '' would work
value: string,
inputType: string
}];
constructor() {
this.felder.push({elementId: 'ID1', value: 'First', inputType: 'input'});
this.felder.push({elementId: 'ID2', value: 'Second', inputType: 'dropdown'});
this.felder.push({elementId: 'ID3', value: 'Third', inputType: 'checkbox'});
}
But it does not work. The error message is:
string' only refers to a type, but is being used as a value here.
I understand the error but cannot find a solution. How can I define a must-used datatype?
Thanks & Best Regards
1 Answer
Use an interface:
interface MyInterface {
elementId: string;
value: string;
inputType: string;
}
class MyClass {
public felder: MyInterface[] = [];
constructor() {
this.felder.push({elementId: 'ID1', value: 'First', inputType: 'input'});
this.felder.push({elementId: 'ID2', value: 'Second', inputType: 'dropdown'});
this.felder.push({elementId: 'ID3', value: 'Third', inputType: 'checkbox'});
}
}