typescript / beginner
Snippet
Array Type Annotations
You can specify the type of elements allowed in an array using the square bracket syntax (type[]) after the type name.
snippet.ts
1
2
const colors: string[] = ["red", "green"];colors.push("blue");
Breakdown
1
const colors: string[]
Declares an array that must exclusively contain string values.
2
["red", "green"]
Initializes the array with two initial string elements.
3
colors.push("blue");
The push method is type-checked; adding another string is permitted.