What benefits could you derive from utilizing an interface to generate a fresh Array? (Pulumi)

Just delving into the world of TypeScript and Pulumi/IaC. I'm trying to wrap my head around a code snippet where an array of key values is being created using an interface:

import * as cPulumi from "@company/pulumi";

interface TestInterface {
    name: cPulumi.output<string>,
    description: cPulumi.output<string>,
    password: cPulumi.output<string>
}

const allVm = {
    vm1: new Array<TestInterface>(),
    vm2: new Array<TestInterface>(),
    vm3: new Array<TestInterface>()
}

export { allVm }

Am I correct in understanding that this code is creating arrays for each "vm" with elements conforming to the TestInterface format? Later on, there seems to be code that inserts multiple objects following the structure of the interface into "vm1."

Answer №1

You essentially provided the solution to your own query by explaining that it simply generates an empty array with a specific item-type defined in the generics. The same piece of code (assuming no overriding of the Array constructor) can also be expressed as:

const allVm = {
    vm1: [] as TestInterface[],
    vm2: [] as TestInterface[],
    vm3: [] as TestInterface[],
}

Alternatively, it could be written as

const allVm: { vm1: TestInterface[], vm2: TestInterface[], vm3: TestInterface[] } = {
    vm1: [],
    vm2: [],
    vm3: [],
}

The usage of new Array<Type> may be more readable and is particularly recommended in projects where type casting like [] as TestInterface[] is restricted by linter guidelines.

TS Playground

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a 3D image array in NumPy to a 2D array

I am working with a 3D-numpy array representing a grayscale image. Here is an example of how it looks: [[[120,120,120],[67,67,67]]...] Since it is a gray image, having every R, G, and B value the same is redundant. I am looking to create a new 2D array t ...

What are the steps for retrieving a JSON object within an array?

var jsonData = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue" :{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text" :"Size32","price":"22","isSelec ...

The ngOnInit child function fails to activate when trying to assign a fresh object

On the parent page, I am passing user data to a child component in this way: <ng-container *ngIf="leaderboard"> <app-leaderboard-preview [user]="user" (click)="goToLeaderboard()"></app-leaderboard-preview> </ng-container> In t ...

Error encountered: Segmentation Fault when executing the following C program

When running this C program with planet names as arguments, it correctly identifies whether they are planets or not. For example: ./planets Venus Mercury However, introducing a wrong case results in a Segmentation Fault: ./planets Venus Mercury mercur ...

Access information from JSON file

Looking to extract data from an external JSON file and store it in a JavaScript array for manipulation? Here is a snippet of the JSON file for reference: "Country":[ { "Country_Name":"India", "Country_Details":[ { ...

The parameter 'EventTypes' cannot be assigned to a type of string

I am working on enhancing the functionality of the function provided below by adding types to it, clickEvent(event:Event) { this.event = event } The HTML Code: <a [href]="href" [target]="target" (click)="clickEvent('text')"></ ...

Discover the best method for retrieving or accessing data from an array using Angular

In my data processing task, I have two sets of information. The first set serves as the header data, providing the names of the columns to be displayed. The second set is the actual data source itself. My challenge lies in selecting only the data from th ...

Triggering a client-side dialog in Electron-Angular upon receiving an IPC event

I am experiencing a strange issue with my back-end notification system and client-side Angular Material dialog component. There are times when the dialog does not fully instantiate, even though the constructor of the component is invoked. The component&apo ...

Is there a way to make the Sweetalert2 alert appear just one time?

Here's my question - can sweetalert2 be set to only appear once per page? So that it remembers if it has already shown the alert. Swal.fire({ title: 'Do you want to save the changes?', showDenyButton: true, showCancelButton: true, ...

Here is a unique rewrite of the text: "Converting to TypeScript: Add the Node Modules Path to the global paths array in the 'module' module using

Utilizing vue ui, I initiated a vue project with typescript and subsequently integrated electron, iview, and less... After addressing the lexical issues in the *.ts files, running the vue-cli-service serve task in vue ui yielded the following output: Tot ...

Modifying the pointer within an array does not result in the modification of the original pointer

Having a ball with one adjacent ball, my struggle lies in updating the value of ball 2 which is stored in ball1's adjacent. It seems that updating ball 1's adjacent doesn't reflect the changes in actual ball 2. struct ball { stru ...

Importing BrowserAnimationsModule in the core module may lead to dysfunctional behavior

When restructuring a larger app, I divided it into modules such as feature modules, core module, and shared module. Utilizing Angular Material required me to import BrowserAnimationsModule, which I initially placed in the Shared Module. Everything function ...

The ng-model-options in Angular 2 is set to "updateOn: 'change blur'"

Currently working with angular 2, I am seeking a solution to modify the behavior of ngModel upon Enter key press. In angular 1.X, we utilized ng-model-options="{updateOn: 'change blur'}". How can this be achieved in angular 2? For reference, her ...

Error in Keras input shape - providing entire array instead of individual lines

Currently, I am working on loading images from a CSV file. The images in the file are originally 300 x 300 pixels, but are flattened to 90000 pixels. I encountered an error related to the input shape while using the TensorFlow backend. I have provided link ...

Develop a JSON object with a unique format by combining elements from two separate arrays using JavaScript

I've searched extensively on stack for a solution but couldn't find one, so I'm reaching out here for help: Let's consider two arrays: one with "keys" and the other with "values" For example: keys = [CO2, Blood, General, AnotherKey, . ...

MQTT Broker specialized in Typescript

I'm looking to create a MQTT Broker using TypeScript and Angular. I've attempted a few examples, but I keep encountering the following error: Uncaught TypeError: http.createServer is not a function Here's a simple example of what I'm c ...

Uncover the value type of a key in TypeScript without using a discriminated union

I want to implement a type map that ensures the type of an object's value for a specific key is determined by the value of another key. For example: type CurrencyValue = { code: string; value: number; }; type FieldMap = { text: string; curren ...

Vue3 - changes to array data not triggering automatic re-rendering

Being relatively new to Vue, I'm struggling to automatically update the array in my project. My webpage is simple - it consists of an input field and a button. When the button is clicked, the data is saved in a MongoDB database through my server. Be ...

When I use React Axios to make a request, I am unable to see the cookie

While I understand this topic has been covered extensively, I have exhausted all possible solutions and still can't resolve the issue. Here is my code for handling a login request in React using Axios: async login(email: string, password: string) { ...

Load the workspace's current state when opening a VS Code extension

Recently, I've been developing a VS Code extension that creates a button in the status bar to execute a script in the terminal. The state of these buttons and custom user commands is saved in context.workspaceState. However, I've encountered an ...