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

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'}); } } 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy