Hello, I am trying to create a function that can determine the primitive type of an array. However, I am facing an issue and haven't been able to find a solution that fits my problem. Below is the function I have written:
export function isGenericTypeArray<T>(value: any): value is T[] {
// Avoid using on classes!!!!!!!!!!!!!!!!!!!!!!
if (Array.isArray(value)) {
let somethingIsOfType = false;
value.forEach(function (item) {
if (typeof item !== typeof T) {
console.log("array isn't of matching type!");
return somethingIsOfType; // false
}
});
somethingIsOfType = true;
return somethingIsOfType; // true
}
console.log("this isn't an array");
return false;
}
The error lies in the line if (typeof item !== typeof T)
, it gives me 'T' only refers to a type but is being used as a value here.ts(2693).
I came across this post on Stack Overflow but couldn't comprehend it fully.
I also stumbled upon this post on Stack Overflow but it didn't provide clarity for my issue.