Dynamically defined type literals in Typescript

Recently, I came across an intriguing problem. I am attempting to develop a Vue.js wizard component that takes in configuration objects. The type of demo I have looks like this:

type WizardConfiguration = {
  steps: Array<{
      name: string,
      fields: {
        fieldName: string;
      }[]
    watchers: (FIELD NAMES ARR) => void
  }>,
  globalWatchers: (ENTIRE STATE FIELD NAMES ARR) => void
}

The issue arises with the watchers line. I want the watchers callback to receive an array of field names specified in the fields array. An example would be:

const config: WizardConfiguration = {
  steps: [
   {
      name: "Step 1",
      fields: [
         { fieldName: "Field 1 1" },
         { fieldName: "Field 1 2" }
      ],
      watchers: ([HERE I GET A INTELLISENSE of "Field 1 1", "Field 1 2"]) => {
      // do smth
      }
   },
   {
      name: "Step 2",
      fields: [
         { fieldName: "Field 2 1" },
         { fieldName: "Field 2 2" }
      ],
      watchers: ([HERE I GET A INTELLISENSE of "Field 2 1", "Field 2 2"]) => {
      // do smth
      }
   },
  ],
  globalWatchers: ([HERE I GET INTELLISENSE OF ENTIRE WIZARD STATE - "Field 1 1", "Field 1 2", "Field 2 1", "Field 2 2"])
};

Is it possible to achieve something like this currently in TypeScript?

Thank you.

EDIT 1: The wizard will maintain the state as a reactive object. I aim for the simplest object structure possible, where each fieldName in the wizard is represented by [fieldName]: value in the state object.

Answer №1

There isn't a specific TypeScript type that directly fits your requirements. Instead, you can approach it by creating a generic type like

WizardConfiguration<T extends any[][]>
. Here, T represents a tuple containing tuples of strings. For instance,
WizardConfiguration<[["a","b"],["c","d"]]>
describes a configuration with two steps: the first step contains fields "a" and "b", while the second step includes fields "c" and "d".

The structure of this type could be defined as follows:

interface WizardConfiguration<T extends string[][]> {
   steps: { [I in keyof T]: Step<T[I]> },
   globalWatchers: (args: TupleFlat<T>) => void;
}

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

Anticipate that the function will remain inactive when the screen width is less than 480 pixels

Implementing Functionality in Responsive Navigation <script> document.addEventListener('DOMContentLoaded', () => { let windowWidth = window.Width; let withinMobile = 480; let close = document.getElementById('close&ap ...

Dynamically fetching and uploading files from a specific path using Node.js, Express, and Angular 1.x

How can I upload or move all files from a specific folder using NodeJS, Express, and Angular 1.x by providing the folder path? What is the best way to handle this operation in either Angular or Node? Should I use: var fs = require('fs') module ...

AngularJS input range is written inside the bubble that crosses over the screen

Is there a way to write inside the bubble of the range slider? I have adjusted the design of the range slider and now I simply want to display the number inside the bubble: Please see image for reference I aim to display the number inside a circle. What ...

When trying to update a Mobx store observable from within itself, you may encounter an error message stating "Unable to access property of <observable> as it is undefined."

Although I am aware of similar issues that have been discussed before, I couldn't find a solution to my specific problem after reviewing many of them. Let me explain the situation: In my scenario, I need my ReportStore to retrieve data from the serve ...

Following a Node/Npm Update, Sails.js encounters difficulty locating the 'ini' module

While developing an application in Sails.js, I encountered an authentication issue while trying to create user accounts. Despite my efforts to debug the problem, updating Node and NPM only resulted in a different error. module.js:338 throw err; ...

How can I retrieve the row index in an Angular Mat-Table that has expandable content?

Having a mat-table with expandable content on a page, I am seeking a solution to record the row number of the table when clicked. While I successfully achieved this with a regular table in the HTML file using: <tr mat-row *matRowDef="let row; columns: ...

Tips for creating a shift function without using the splice method

I'm currently working on creating custom functions for common array operations. I've hit a roadblock trying to reimplement the shift method without using splice. Any tips or guidance on how to approach this challenge would be highly valued. Cust ...

Managing the jQuery.noConflict function

Upon review, I noticed that the scripts I inherited start like this: var $j = jQuery.noConflict(); The purpose behind this code is not clear to me. While I understand the intent is to avoid conflicts, I am uncertain about the specific conflict it aims to ...

Tips for properly displaying mathematical equations using MathJax in Angular6

I have successfully loaded mathematical equations using the code below. Now, I aim to integrate it within an Angular environment. How can I convert this code for Angular compatibility? <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2. ...

unable to access dropdownlist within the panel

<script type="text/javascript"> function GetSelectedValue() { var selectedValue = document.GetElementById('<%= pnSearch.FindControl("ddlFromFolders").ClientID %>').value; alert(selectedValue); } </script> ...

The filter() function seems to be failing to produce any results even though the callbackFn is functioning properly

I've created a function to filter certain JSON elements in an array using Vue 3. Here is the data structure: [ { "UID_Person": "160a5b9e-c352-39e3-802b-9cfcc126da77", "FirstName": "Maxi", ...

Assemblage of Marionettes: Adding multiple elements to the DOM

I am working on a Marionette application and have come across an issue with inserting new tab items. I have a collection of tabs, but when adding a new item, I need to insert DOM elements in two different areas. The problem is that Marionette.CollectionV ...

What are the common reasons for ajax requests to fail?

Every time I try to run the code with the provided URL, the alert() function in the error: function is triggered. Can someone please assist me with this issue? $("#button").click(function(){ $("#form1").validationEngine(); if($('div input[typ ...

Unable to resolve the Typescript module within a different file

I am in the process of transitioning my React app to TypeScript. Currently, everything is working fine. However, I encountered an issue after adding the TypeScript compiler and renaming files to .ts and .tsx extensions - it is now throwing a "module not fo ...

Retrieving information from a dynamically generated HTML table using PHP

I have successfully implemented functionality using JavaScript to dynamically add new rows to a table. However, I am facing a challenge in accessing the data from these dynamically created rows in PHP for database insertion. Below, you will find the HTML ...

What is the best way to update the displayed data when using Mobx with an observable array?

Is there a way to re-render observable array data in Mobx? I have used the observer decorator in this class. interface IQuiz { quizProg: TypeQuizProg; qidx: number; state: IStateCtx; actions: IActionsCtx; } @observer class Comp extends Rea ...

Moving Cursor Automatically to the End Upon Rejecting Input

I have a form input where users can enter values. I need to validate the inputs to ensure they only contain numbers, dots, and commas. If the input is invalid, I want to prevent it from being stored in the input field. To achieve this, I have implemented ...

How can we translate HTML entities generated by json_encode into matching XML conventions?

As I embark on the journey of creating a Samsung TV App, I find myself working within a strict system that only allows me to use JS and HTML5. One of the challenges I face is the need to send a JSON request to my web server to retrieve data for the emulato ...

Having trouble with loading nested state in ui-router

Despite my efforts to find a solution online, I am stuck on a fairly basic issue. The router/index.html page I am working with appears to be correct, as there are no errors in the console. However, when the URL redirects to the login page, the entire page ...

Utilizing Angular JS to ensure services are universally accessible across controllers and views

Imagine we have a service like this: myApp.factory('FooService', function () { ... Now, from a controller, the code would look something like this: myApp.controller('FooCtrl', ['$scope', 'FooService', function ($s ...