You cannot test for string[] in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330 (I prefer Array.isArray(value)).
If you specifically want for string array you can do something like:
if (Array.isArray(value)) {
var somethingIsNotString = false;
value.forEach(function(item){
if(typeof item !== 'string'){
somethingIsNotString = true;
}
})
if(!somethingIsNotString && value.length > 0){
console.log('string[]!');
}
}
In case you need to check for an array of a class (not a basic type)
if(items && (items.length > 0) && (items[0] instanceof MyClassName))
If you are not sure that all items are same type
items.every(it => it instanceof MyClassName)
Answer from basarat on Stack OverflowYou cannot test for string[] in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330 (I prefer Array.isArray(value)).
If you specifically want for string array you can do something like:
if (Array.isArray(value)) {
var somethingIsNotString = false;
value.forEach(function(item){
if(typeof item !== 'string'){
somethingIsNotString = true;
}
})
if(!somethingIsNotString && value.length > 0){
console.log('string[]!');
}
}
In case you need to check for an array of a class (not a basic type)
if(items && (items.length > 0) && (items[0] instanceof MyClassName))
If you are not sure that all items are same type
items.every(it => it instanceof MyClassName)
Another option is Array.isArray()
if(! Array.isArray(classNames) ){
classNames = [classNames]
}
Videos
There is a comment made by the user @jcalz that explains how it works. It refers to the following code, which is similar to the OP code:
const fruit = ["apple", "banana", "grape"] as const;
export type Fruit = (typeof fruit)[number]; 'apple'|'banana'|'grape';
typeof fruitisArray<"apple" | "banana" | "grape">, soFruitis equivalent to(Array<"apple" | "banana" | "grape">)[number]. The syntaxT[K]means: the type of the properties ofTwhose keys are of typeK. So(Array<"apple" | "banana" | "grape">)[number]means "the types of the properties ofArray<"apple" | "banana" | "grape">whose keys are of typenumber", or: "the array elements ofArray<"apple" | "banana" | "grape">, or:"apple" | "banana" | "grape".
And another similar comment by that user that adds a bit more technical terms:
The type
T[K]is a lookup type which gets the type of the property ofTwhose key isK. In(typeof list)[number], you're getting the types of the properties of(typeof list)whose keys arenumber. Arrays liketypeof listhave numeric index signatures, so theirnumberkey yields the union of all numerically-indexed properties.
typeof gets the type of names variable (which is readonly ['jacob', 'master jung', 'kyuhyun']) then array/tuple member type is resolved. This is called indexed access types or lookup types.
Syntactically, they look exactly like an element access, but are written as types
In this case we "query" the type of tuple member (tuple/array at index) which is 'jacob' | 'master jung' | 'kyuhyun'
Playground
You can use this format to get the member type:
type InputType = typeof input[number];
You can read more about it (and some other cool tips) here in Steve Holgado's blog article.
You can extract it by pointing at an array member, so while you are using the below code to get the type of the array:
type A = typeof input;
You can use this to get the type of a single item:
type B = typeof input[0];