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 OverflowTest for array of string type in TypeScript - Stack Overflow
In Typescript how do you get an element type from an array type, when the array contains different element types? - Stack Overflow
Deriving a type from fields of objects in an array
How to get type from array of objects
Videos
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)
Another option is Array.isArray()
if(! Array.isArray(classNames) ){
classNames = [classNames]
}
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];
I want to create a type restricted to the values used by the objects in an array for a specific type.
I have it working, until I define a type for those objects in the array - then my type becomes just the type of the field in that type.
E.g.
const x = { foo: "foofy" } as const;
const y = { foo: "barfy" } as const;
const z = { foo: "bazfy" } as const;
const xyz = [x, y, z];
type XYZ = typeof xyz[number]["foo"];
// XYZ = "foofy" | "barfy" | "bazfy"But adding a type:
type Fooey = { foo: string };
const a: Fooey = { foo: "foozy" } as const;
const b: Fooey = { foo: "barzy" } as const;
const c: Fooey = { foo: "bazzy" } as const;
const abc = [a, b, c];
type ABC = typeof abc[number]["foo"];
// ABC = stringIs there a way I can have my cake and eat it, here?
typescript playground
TIA!