In TypeScript, which one is right?
[string] vs string[]
public searchOption: [string] = ['date'];
public searchOption: string[] = ['date'];
In TypeScript, which one is right?
[string] vs string[]
public searchOption: [string] = ['date'];
public searchOption: string[] = ['date'];
The first is a tuple and the second is an array of strings.
You can do this with tuples:
let searchOption: [string, number] = ['date', 22];
The second one is right. If you want, you can take a look here https://www.typescriptlang.org/docs/handbook/basic-types.html
These may appear the same, but they're different. As has been mentioned by others, the first is a tuple, and the second is an array of strings.
If your array has multiple strings (eg. ['one', 'two', 'three']) then the second (string[] is correct)
If, like in your original post, you only have one string in the array, then the first ([string]) is correct.