0
0
Typescriptprogramming~5 mins

Array type annotation syntax in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the basic syntax to annotate an array of numbers in TypeScript?
You can annotate an array of numbers using <code>number[]</code>. For example: <code>let nums: number[] = [1, 2, 3];</code>
Click to reveal answer
beginner
How do you annotate an array of strings using the generic Array type syntax?
Use the generic form <code>Array&lt;string&gt;</code>. For example: <code>let names: Array&lt;string&gt; = ['Alice', 'Bob'];</code>
Click to reveal answer
intermediate
Can you mix types in an array annotation? How?
Yes, by using a union type inside the array annotation. For example: <code>let mixed: (number | string)[] = [1, 'two', 3];</code>
Click to reveal answer
beginner
What is the difference between number[] and Array<number>?
Both mean the same: an array of numbers. number[] is shorthand, and Array<number> is the generic form. Use whichever you find clearer.
Click to reveal answer
intermediate
How do you annotate a two-dimensional array of booleans?
You can write <code>boolean[][]</code> or <code>Array&lt;Array&lt;boolean&gt;&gt;</code>. For example: <code>let grid: boolean[][] = [[true, false], [false, true]];</code>
Click to reveal answer
Which of these is a correct TypeScript annotation for an array of strings?
AArray<string>
BBoth B and C
Cstring[]
Dstring()
How would you annotate an array that can hold numbers or strings?
A(number | string)[]
BArray<number | string>
CBoth A and C
Dnumber[] | string[]
What does boolean[][] represent?
AAn array of booleans
BA boolean value
CAn array of arrays of numbers
DA two-dimensional array of booleans
Which syntax is NOT valid for annotating an array of numbers?
AArray<number[]>
BArray<number>
Cnumber[]
DBoth A and B
What is the shorthand syntax for Array<string>?
Astring[]
Bstring()
CArray<string[]>
Dstring{}
Explain how to annotate an array of mixed types in TypeScript.
Think about how to say 'this array can hold more than one type'.
You got /3 concepts.
    Describe the difference and similarity between number[] and Array.
    One is shorter, the other uses angled brackets.
    You got /3 concepts.