I have a simple function which returns array's length
const test = (list: any[]) => list.length;
Which generates eslint error:
no-explicit-any: Unexpected any. Specify a different type. (javascript-eslint)
What would be the correct way to write this function in TypeScript?
Use generics:
const test = <T>(list: T[]): number => list.length;
Then you can call function like:
test([1, 2, 3]) // or test<number>([1, 2, 3]);
test(["s1", "s2", "s3"]); // or test<string>(["s1", "s2", "s3"]);
test([{
key1: 'k1',
key2: 'k2',
}]);