'How to check with typescript checker if type is array of string?

I have an interface in typescript

interface SomeInterface {
   words: string[];
}

I use typescript TypeChecker when compiling to check property types and skip those who are string[]. There is a method in Typescript package to check if node is of array type

function isArrayTypeNode(node: Node): node is ArrayTypeNode;

But is it possible also to check if it is string array?



Solution 1:[1]

Why not your own guard:

function isArrayOfStrings(v: any): v is string[] {
    return v && Array.isArray(v) && v.every((e) => typeof e === "string");
}
  • v - Truthiness
  • Array.isArray(v) - Check if is an array
  • v.every(...) - Check if all elements are strings

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hittingonme