Short version: Ensure you include an =
sign after declaring let flightsData: info
.
In TypeScript, you can define types within the code itself. For example:
let label: string = 'something';
This snippet declares a variable called label with the type string and assigns it a string value.
You can also define more complex structures, such as an Array of strings:
let labels: string[] = ['something', 'something else'];
In your code, the word 'info' is defined as a type representing an Array of TicketInfo objects. You should use it like this:
let flightsData: info = [];
If you leave out the =
symbol, you inadvertently create another type: an Array of info, which is essentially an Array of Arrays of TicketInfo objects:
let flightsData: info[];
Sometimes you can include additional information inside square brackets, which TypeScript can interpret. Consider the following example:
type info = TicketInfo ['id'];
In this scenario, we're not declaring an Array of TicketInfo objects. Instead, we're specifying the 'id' property of TicketInfo, which is a number, making the type info a number as well.
In your specific situation, the absence of the =
sign in your code resembles the previous example, but using {} as an index (similar to 'id' in TicketInfo['id']) results in an error.
let flightsData: info [
{
}, ]
This syntax attempts to access the index {}, which is invalid within an Object. {} cannot be a valid index.