0

I have an input to my component that I declared like this.

@Input() rows: (string | number)[][];

I understand this is an array of strintegers (string or number), which I have an array of. So the data is a matrix, two dimensions, with strintegers (of floaties but in my case, it's not).

Realizing that not each row will have the same element count, I had to re-work the matrix into a dictionary, i.e. an array of objects (where the objects have properties we know nothing about, except that those are strintegers only). This is my best attempt.

@Input() rows: { [key: string]: (string | number) }[];

While the computer seems to accept it, I fear two things. First one being that it's plain wrong syntax and that I'm only ignorant of the issue due to poor testing. Second one being that regardless of correctness, I've created an atrocious enigma no eyes should ever behold (i.e. there's a perfectly neat way to express the same data structure).

How would one go about declaring a type as specified above in a better way than the one suggested?

wentjun
  • 40,384
  • 10
  • 95
  • 107
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 2
    Nothing wrong with above type. You could use a "shortcut" `Record[]` but that's really the same – Aleksey L. Jan 05 '20 at 15:55
  • @AlekseyL. While thankful for the feedback and seeing where you're coming from, I would very much prefer to include your suggestion as a part of an accepted answer. I'm going to snatch it and amend it to one below for future readers' sake. That way, they can decide themself whether an dedicated interface or a general TS type. – Konrad Viltersten Jan 05 '20 at 17:45
  • Sure, go ahead – Aleksey L. Jan 05 '20 at 17:47

2 Answers2

3

It seems like valid TypeScript to me. If I were you, I would clean up the typings such that I would declare an interface which could be reused in other components (perhaps the parent, which is the source of the input, or child, if you decide to further pass it on?).

export interface Row {
  [key: string]: string | number;
}

And then, within your component itself, you can import the interface and use it to declare the input typings.

@Input() rows: Row[];

Another approach, as suggested by @aleksayl., would be to apply a default GP type from TS like so.

Record<string, string | number>[]
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
wentjun
  • 40,384
  • 10
  • 95
  • 107
-1

Your code is perfectly fine, don't worry about it. However, it can be a good idea to move your model into an interface, like such:

interface Row {
  [key: string]: string | number;
}

As a side note: your syntax cannot be 'plain wrong' if your compiler/transpiler accepts it as valid Typescript - that doesn't always mean there isn't a better way however.

Ali Nasserzadeh
  • 1,365
  • 7
  • 15
  • I disagree with the statement that (a) compiler accepts the syntax necessarily implies (b) nothing is wrong. While I understand that it's a good start, I rest in humble precaution that I might have introduced a behavior that doesn't show any problems because I'm (unintentionally) testing it with special cases that conceal the true FUBAR'ness. Usually, it turns out to be needless worry but I rather be excessively careful than arrogantly mistaken. – Konrad Viltersten Jan 05 '20 at 17:39
  • there might be a problem with your code, yes but your syntax cannot be wrong unless the compiler does not accept it. Syntax errors are caught at compile/transpile time – Ali Nasserzadeh Jan 05 '20 at 18:49
  • I believe you're missing the point. As stated in the question, it's not about whether the syntax was acceptable by the computer. That, I can check by simply running the program. The question here is about the appropriateness and smoothness of the chosen syntax. I really don't need StackOverflow to ask if syntax works, obviously. If you check the other answer, you can see what kind of info I was asking about. The question wasn't a simple "how to" but rather an advanced best-practice one. If a programmer is satisfied because the computer accepts the code... not much of a programmer, then... – Konrad Viltersten Jan 06 '20 at 00:16
  • My point is that your syntax cannot be plain wrong, if the compiler accepts it as you have stated. Once the compiler accepts your syntax, the syntax isn't wrong, only your semantics can be wrong, if you want to know more about it, this is a pretty good answer covering your confusion on the matter: https://stackoverflow.com/questions/17930267/what-is-the-difference-between-syntax-and-semantics-in-programming-languages – Ali Nasserzadeh Jan 06 '20 at 10:19