0
0
Typescriptprogramming~5 mins

Generic array syntax in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the generic array syntax in TypeScript?
It is a way to define arrays with a specific type using angle brackets, like Array<Type>. For example, Array<number> means an array of numbers.
Click to reveal answer
beginner
How do you declare an array of strings using generic array syntax?
You write <code>let names: Array&lt;string&gt; = ['Alice', 'Bob'];</code> which means <strong>names</strong> is an array that only holds strings.
Click to reveal answer
beginner
What is the difference between string[] and Array<string> in TypeScript?
Both mean the same: an array of strings. string[] is shorthand, and Array<string> is the generic syntax. You can use either based on preference.
Click to reveal answer
intermediate
Can you use generic array syntax with custom types?
Yes! For example, if you have a type Person, you can write Array<Person> to make an array of Person objects.
Click to reveal answer
intermediate
Why use generic array syntax instead of the shorthand?
Generic syntax is clearer when working with complex types or nested generics. It helps readability and consistency in some cases.
Click to reveal answer
Which of the following declares an array of numbers using generic array syntax?
Alet nums: number<> = [1, 2, 3];
Blet nums: number = [1, 2, 3];
Clet nums: Array = [1, 2, 3];
Dlet nums: Array<number> = [1, 2, 3];
What does Array<string> mean in TypeScript?
AAn array of strings
BA function returning a string
CA string variable
DAn array that can hold any type
Which is the shorthand for Array<boolean>?
Aboolean[]
Bbool[]
CArray<boolean[]>
Dboolean<>
Can you use generic array syntax with a custom type Car?
ANo, only built-in types work
BOnly if Car is a string
CYes, like <code>Array&lt;Car&gt;</code>
DOnly with interfaces, not types
Why might you choose Array<Type> over Type[]?
AIt is required by TypeScript
BIt is clearer with complex or nested types
CIt is shorter to write
DIt runs faster
Explain how to declare an array of numbers using generic array syntax in TypeScript.
Think about how you tell TypeScript what type the array holds using < and >.
You got /4 concepts.
    Describe the difference and similarity between string[] and Array<string>.
    Both are ways to say 'array of strings' but look different.
    You got /3 concepts.