Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript - [string] vs string[]

Tags:

In typescript, What is right thing?

[string] vs string[]

public searchOption: [string] = ['date']; public searchOption: string[] = ['date']; 
like image 893
Jin-Kwon Lee Avatar asked Apr 05 '17 08:04

Jin-Kwon Lee


People also ask

What is string [] in TypeScript?

In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string.

What is the difference between string [] and string?

String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter ( String... ) you can call the method like: public void myMethod( String... foo ) { // do something // foo is an array (String[]) internally System.

Is string [] the same as array string?

There's no difference between the two, it's the same.

Should I use string or string in TypeScript?

TypeScript: String vs string 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.


2 Answers

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]; 
like image 93
Saravana Avatar answered Sep 19 '22 17:09

Saravana


The second one is right. If you want, you can take a look here https://www.typescriptlang.org/docs/handbook/basic-types.html

like image 22
geo Avatar answered Sep 20 '22 17:09

geo