0

Suppose I have two classes Foo and bar, each class has an attribute called fields such as

// Foo
fields = {
    id: {
        type: 'number',
        display: 'ID'
    },
    name: {
        type: 'string',
        display: 'Name'
    }
    // ...
}

// Bar
fields = {
    version: {
        type: 'number',
        display: 'Version'
    },
    valid: {
        type: 'boolean',
        display: 'Valid'
    }
    // ...
}

Fields in these classes have different keys, but each key value has the same type with type and display. Is there a way to define a type or interface for fields?

1 Answer 1

2

If you don’t need to limit what the keys can be called you can define a Record with keys of string and values of an object with type and display fields:

type MyType = {
  fields: Record<string, {
    type: 'boolean' | 'number' | 'string';
    display: string;
  }>;
};

interface MyInterface {
  fields: Record<string, {
    type: 'boolean' | 'number' | 'string';
    display: string;
  }>;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Record is exactly what I have been looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.