Back to questions
JuniorTheoryCommon
On which line does tsc report an error?
function longest<T extends { length: number }>(a: T, b: T): T {
return a.length >= b.length ? a : b;
}
console.log(longest("cat", "horse"));
console.log(longest(10, 200));6
- Expecting the error in the declaration: the function is correct, and tsc reports the call whose argument breaks the constraint.
- Expecting strings to fail because they are not objects: a constraint asks only for the properties, and every string has
length.
- Why does
a.lengthcompile insidelongest, whilea.toUpperCase()would not? - What does
longest([1, 2], [3])return, and what is its type?