You can mix checks and assertions. If a value is an array, you can still assert its element type when you know it.
Here the input is unknown. If it is an array of numbers, return the double of the first number. Otherwise return 0.
function firstDouble(a: unknown) {
if (Array.isArray(a)) {
const n = (a as number[])[0];
return typeof n === "number" ? n * 2 : 0;
}
return 0;
}
Return the length of the first string when the input is a string array.