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 Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
For example, TypeScript knows that only a string value will have a typeof value "string": ... Notice that in the else branch, we don’t need to do anything special - if x wasn’t a string[], then it must have been a string. Sometimes you’ll have a union where all the members have something in common. For example, both arrays ...
🌐
Graphite
graphite.com › guides › typescript-typeof-operator
TypeScript typeof operator - Graphite
When used with an array, typeof will return the type of the elements inside the array along with an array indicator ([]).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › isArray
Array.isArray() - JavaScript - MDN Web Docs
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015 · Array.isArray() checks if the passed value is an Array. It performs a branded check, similar to the in operator, for a private field initialized by the ...
🌐
TypeScript Tutorial
typescripttutorial.net › home › typescript tutorial › typescript array type
TypeScript Array Type
August 24, 2024 - Since an element in a string array is a string, TypeScript infers the type of the skill variable to string as shown in the output.
🌐
Total TypeScript
totaltypescript.com › get-the-type-of-an-array-element
This Crazy Syntax Lets You Get An Array Element's Type | Total TypeScript
September 2, 2024 - Learn how to extract the type of an array element in TypeScript using the powerful `Array[number]` trick.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-get-types-from-arrays-in-typescript
How To Get Types From Arrays in TypeScript? - GeeksforGeeks
September 26, 2024 - The typeof operator allows us to know the type of an array's elements by referencing the array variable directly.
Find elsewhere
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-array
TypeScript Arrays
An array in TypeScript can contain elements of different data types using a generic array type syntax, as shown below.
🌐
Medium
medium.com › @mehdi.havaei77 › whats-the-meaning-of-typeof-array-number-in-typescript-7fa6b53120f0
What’s the meaning of typeof Array[number] in Typescript? | by Mehdi Havaei | Medium
September 29, 2023 - The type of the properties of T whose keys are of type K. So (Array<”apple” | “banana” | “peach”>)[number] means the types of the properties of Array<”apple” | “banana” | “peach”> whose keys are of type number, OR : the array elements of Array<”apple” | “banana” | “peach”>, or: ”apple” | “banana” | “peach” · The type T[K] is a lookup type which gets the type of the property of T whose key is K. In (typeof list)[number], you’re getting the types of the properties of (typeof list) whose keys are number.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › indexed-access-types.html
TypeScript: Documentation - Indexed Access Types
Another example of indexing with an arbitrary type is using number to get the type of an array’s elements. We can combine this with typeof to conveniently capture the element type of an array literal:
🌐
TkDodo
tkdodo.eu › blog › array-types-in-type-script
Array Types in TypeScript
August 19, 2023 - To be clear, there is absolutely no functional difference between the two notations. It seems that its just personal preference about which one to choose. Whatever you do, please make sure to turn on the array - type (opens in a new window) eslint rule to use either one of the notations consistently.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 28046
Creating types from values in array · Issue #28046 · microsoft/TypeScript
October 22, 2018 - const values = { A: 'A', B: 'B' } type Foo = keyof typeof values const v1: Foo = 'A' const v2: Foo = 'D' // Type '"D"' is not assignable to type '"A" | "B"' ... Link to playground http://www.typescriptlang.org/play/#src=let vals1 = ['A', 'B'] type Foo1 = OneOf<vals1> // Is there a way of doing this?
Author   G2Jose
🌐
W3Schools
w3schools.com › typescript › typescript_arrays.php
TypeScript Arrays
The readonly keyword can prevent ... the readonly modifier and see if it works? Try it Yourself » · TypeScript can infer the type of an array if it has values....
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-array-element-type
Get the Element type from an Array type in TypeScript | bobbyhadz
Use a condition type with an infer declaration to infer the type of an element in the array. TypeScript will fill in the type of the element and we can return it in the true branch of the conditional type.
Top answer
1 of 4
33

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 fruit is Array<"apple" | "banana" | "grape">, so Fruit is equivalent to (Array<"apple" | "banana" | "grape">)[number]. The syntax T[K] means: the type of the properties of T whose keys are of type K. So (Array<"apple" | "banana" | "grape">)[number] means "the types of the properties of Array<"apple" | "banana" | "grape"> whose keys are of type number", or: "the array elements of Array<"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 of T whose key is K. In (typeof list)[number], you're getting the types of the properties of (typeof list) whose keys are number. Arrays like typeof list have numeric index signatures, so their number key yields the union of all numerically-indexed properties.

2 of 4
6

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

🌐
Medium
medium.com › geekculture › 5-ways-to-judge-array-in-typescript-dfeb1d9c310b
5 Ways to Judge Array in TypeScript | by Zachary | Geek Culture | Medium
March 21, 2022 - When the call to if (isArray(testData)) passes, TypeScript will reduce the variable type to the concrete Array<unknown> type in the next branch scope, conversely, in the else branch, it will is not of type Array<unknown>. Ok, let’s take a look at the logic of the isArray method. This judgment logic is correct, but once the constructor of the array is modified, it is impossible to make a correct judgment. function isArray(arr: unknown): arr is Array<unknown> { return typeof arr === 'object' && arr?.constructor === Array; }const testData: unknown = [1, 2, 3]; // @ts-ignore testData.constructor = null; console.log(isArray(testData)); // false
🌐
Fjolt
fjolt.com › article › typescript-array-type
TypeScript Array Type
July 9, 2022 - If we have an array, we can define its type in TypeScript by using the notation type[]. For example, the below variable arrayType is expected to be an array of strings.
🌐
LinkedIn
linkedin.com › posts › mapocock_typeof-arraynumber-is-a-classic-trick-activity-7150092641546752000-SfWp
Matt Pocock on LinkedIn: (typeof array)[number] is a classic trick to extract the type of the… | 25 comments
January 8, 2024 - Author of Total TypeScript 2mo · Report this post · (typeof array)[number] is a classic trick to extract the type of the members of an array. 1,070 25 Comments · Like · Comment · Share · Copy · LinkedIn · Facebook · Twitter · Pathum ...