Retrieve the value of a property within the same interface

Is there a way to access an interface prop value within the same interface declaration in order to dynamically set types?

I am attempting something like this:

export type MethodNames = "IsFallmanagerUpdateAllowed" | "UpdateStammFallmanager";

export interface IsFallmanagerUpdateAllowed {
  plcParameter : StammFallmanagerParameter
}

export interface UpdateStammFallmanager {
  plcParameter : StammFallmanagerParameter
}

export interface ServerTaskParam<T> extends system.ServerTaskParam<T> {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : // here depending on the passed methodname type should be IsFallmanagerUpdateAllowed or UpdateStammFallmanager
  // paramObj   : T -> this is what I use atm but I want to make it more dynamic
}

Note that there could be more MethodNames.

The goal is for intellisense to indicate directly which type of object should be used as paramObj when passing name and methodName.

If possible, it should be something like this:

export interface ServerTaskParam extends system.ServerTaskParam {
  name       : `${Class.NAME}`,
  methodName : MethodNames,
  paramObj   : [ methodName ] -> use methodName value to refer to one or the other interface in the same namespace (pseudo syntax)
}

I have been searching online for solutions but haven't found anything. Is this concept feasible?

Answer №1

I believe a union is what you're looking for:

export type ServerTaskParam = ({
  name       : `${ClassName}`,
  methodName : "IsFallmanagerUpdateAllowed",
  paramObj   : IsFallmanagerUpdateAllowed
} | {
  name       : `${ClassName}`,
  methodName : "UpdateStammFallmanager",
  paramObj   : UpdateStammFallmanager
}) & system.ServerTaskParam

Playground

Answer №2

To streamline your code, consider creating a composite type that encompasses all possible interfaces. This "higher type" allows you to utilize its keys in the following manner:

export interface IsUpdatePermitted {
  parameter : ManagerParameters
}

export interface UpdateManager {
  parameter : ManagerParameters
}

interface Actions {
  IsUpdatePermitted: IsUpdatePermitted;
  UpdateManager: UpdateManager;
}

export interface TaskParameter<K extends keyof Actions> extends system.TaskParameter<T> {
  name       : `${ClassName}`,
  methodName : K,
  paramObject   : Actions[K]
};

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

How can I display and link a base64 string to an Image as a source in Nativescript?

I'm having trouble displaying and binding a base64 image as an ImageSource in my View. The image doesn't show up at all, and I couldn't find any helpful information about it in the documentation. Am I missing something? The imageSource prop ...

What is the best way to call a method within a TypeScript class using its name as a string while already inside the class itself?

Currently, I am developing a class that automates the creation of routes for Express and invokes a function in a controller. However, upon trying to execute this[methodName](req, res), I come across an error message stating: 'Element implicitly has an ...

Encapsulating constructor variables in TypeScript classes through private access modifiers and using public getters

Coming from a background in C#, I am used to designing most of my classes to be immutable. I am curious about whether it is considered good practice in TypeScript to use private constructor variables and public getters for accessing data within classes. T ...

Tips for monitoring changes to files while developing a NestJs application within a Docker container

Having an issue with NestJS and Docker here. Trying to run the development script using npm start: dev, but encountering a problem where the app runs fine but doesn't detect any changes in the source files, hindering the development process. Here&apo ...

Utilizing TypeScript's dynamic class method parameters

I've been working on integrating EventBus with TypeScript and I'm trying to create a dynamic parameter event in the emit method. How can I achieve this? interface IEventBusListener { (...params: any[]): void } class EventBus { constructo ...

Unit Testing Angular: Passing FormGroupDirective into a Function

I am currently writing unit tests for a function that takes a parameter of type FormGroupDirective. I have been able to test most of the logic, but I'm unsure about what to pass as a parameter when calling the resetForm() function. Here is the code sn ...

Issue: The data type 'void' cannot be assigned to the data type 'ReactNode'

I am encountering an issue while calling the function, this is just for practice so I have kept everything inside App.tsx. The structure of my class is as follows: enum Actor { None = '', } const initializeCard = () => { //some logic here ...

I encountered an issue where I did not receive a response when utilizing res.write() within the fetch function

Currently, I am utilizing the <res.write()> method in nodejs at https://nodejs.org/api/http.html#responsewritechunk-encoding-callback. In addition to this, I am also implementing the fetch function which can be found at https://developer.mozilla.org/ ...

Implementing TypeScript type definitions for decorator middleware strategies

Node middlewares across various frameworks are something I am currently pondering. These middlewares typically enhance a request or response object by adding properties that can be utilized by subsequent registered middlewares. However, a disadvantage of ...

A guide on implementing RxJS Observables to target a specific DIV element

Currently, I am working with Angular 2. At the moment, I have been using this method to select a specific DIV element: <div #aaa> </div> @ViewChild('aaa') private aaa: ElementRef; ngAfterViewInit() { let item = this.aaa.nativeEle ...

angular2 ngFor is not functioning properly

I'm having an issue where I cannot get textboxes to appear whenever a user clicks a button. I am attempting to achieve this using ngFor, but for some reason, the ngFor is not iterating as expected. Even after trying to change the array reference with ...

What is the importance of having a reference path for compiling an AngularJS 2 project using gulp-typescript?

I wanted to modify the Angular Tour Of Heros project to utilize gulp from this Github Repository. This is the gulpfile.json file I came up with: const gulp = require('gulp'); const del = require('del'); const typescript = require(&apo ...

A guide to using Angular to emphasize text based on specific conditions met

Currently, I am developing a testing application that requires users to choose radio type values for each question. The goal is to compare the selected answers with the correct answers stored in a JSON file. To achieve this, I have implemented an if-else ...

Returning a 'never' type from a function in React using Typescript

Basically, I have a function that initiates the OAuth flow (redirecting to Google OAuth login page, for example): async function signIn() { // start OAuth flow } And let's say I want to use it in a useEffect hook like this: ... useEffect(() => { ...

Troubleshooting TypeScript Modules in Visual Studio 2015 Update 2: Fixing the 'require' Undefined Error

While working in Visual Studio 2015 Enterprise with Update 2 installed, I decided to create a new TypeScript project called TypeScriptHTMLApp1 using the default template and settings. As part of this project, I added a new TypeScript file named log.ts and ...

The SonarQube code coverage differs from that of Jest coverage

I'm struggling to grasp why SonarQube and Jest coverage results differ so significantly. SonarQube coverage resultshttps://i.sstatic.net/dodGi.png Jest coverage https://i.sstatic.net/uYKmt.png https://i.sstatic.net/rakzw.png https://i.sstatic.net/ ...

Issue with binding nested ViewModels/components in Knockoutjs using TypeScript does not resolve

Struggling with implementing a viewModel within another viewModel in knockout. Any assistance would be greatly appreciated. Using typescript and aiming to have a list of address controls, each with their individual viewmodel. Initially, the project functi ...

Utilize Angular 4 to effectively update objects within Firebase Cloud Firestore

Hey there! I've been working with firebase and angular 4 on this new thing called firestore. I've been trying to update one of the documents, but I keep encountering this error. Here's my component: When I remove the interfaces, meaning if ...

Setting up systemjs.config.js for utilizing relative paths within IIS - A step-by-step guide

My new ASP.NET MVC web application utilizes Angular for its UI components. I have set up the necessary config files in my project (e.g. package.json and systemjs.config.js). Created a test page Index.cshtml to test out the template in app.component.ts. The ...

Extracting information from an object retrieved through an http.get response can be accomplished by utilizing various methods and

I am working with an API that returns a JSON object like this: { "triggerCount": { "ignition_state_off": 16, "ignition_state_on": 14, "exit_an_area": 12, "enter_an_area": 19, "door_unlocked": 1, "door_l ...