typescript / beginner
Snippet
Array Type Syntax
You can define an array that only allows a specific type of element by adding square brackets after the type name.
snippet.ts
1
2
3
const fruits: string[] = ["Apple", "Banana", "Orange"];fruits.push("Mango");
Breakdown
1
fruits: string[]
Defines an array that can only contain strings.
2
["Apple", "Banana", "Orange"]
Initializes the array with three string elements.
3
fruits.push("Mango");
Adds a new string to the array; TypeScript verifies this is allowed.