0

I have an object for an array that can be the following

{
 'value1': 1,
 'value2': 2
}

or

{
 'value1': 'a',
 'value2': 'b'
}

or

{
 'value1': {'sub1': 1, 'sub2': 2},
 'value2': {'sub1': 1, 'sub2': 2}
}

I wanted to type this like this :

export interface TableRow {
  [key: string]: string | number | ([key: string] : string) | ([key: string] : number)
}

but it doesn't work.

Is this the only way possible ?

export interface TableRow {
  [key: string]: string | number | object
}
1
  • This looks like just a typo to me (e.g., interface TableRow { [key: string]: string | number | { [key: string]: string } | { [key: string]: number } }). Is that what you mean by "it doesn't work"? Usually a minimal reproducible example should explicitly describe what the problem is Commented May 21, 2020 at 3:15

2 Answers 2

2

i think it better to do this neatly with two interfaces

export interface TableCell {
    [key: string]: string | number
}

export interface TableRow {
    [key: string]: string | number | TableCell
}
Sign up to request clarification or add additional context in comments.

Comments

1

Replace () with {}:

export interface TableRow {
    [key: string]: string | number | { [key: string]: string } | { [key: string]: number }
}

Comments

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.