Back to questions
JuniorTheoryCommon
What does tsc report for this program?
type Pair<T> = [T, T];
const p: Pair<number> = [1, "2"];
console.log(p[0], p[1]);An error: the second element "2" does not fit number
- Expecting
Tto be inferred from the value: in a type annotation the type argument is written, and the array is checked against[number, number]. - Counting type arguments by the uses of
T:Pairdeclares one parameter, soPair<number>fills both places of[T, T].
- How would you declare a pair whose two elements may have different types?
- What does tsc report for
const q: Pair = [1, 2];?