Is it possible to retrieve the signature for every method within a class?

Let's consider a scenario where we have a class defined as follows:

class Actions {
  static FooAction = 'foo' as const;
  someAction1() {
    return {
     type: Actions.FooAction,
     payload: {a: 1, b:2}
  }}

  static BarAction = 'bar' as  const;
  someAction2() {
    return {
     type: Actions.BarAction,
     payload: {c: 3, e:2}
  }}
  ... and so on ...
}

All the methods within this class return an object with a similar structure:

{type: string, payload: Record<string, number>}

However, to enforce stricter typing, we want it to be:

type ReturnedActions = 
| { type: 'foo', {a: string, b:string} } 
| { type: 'bar', {c: string, e:string} } 
...

This allows us to use a switch statement to filter by type:

declare var a: ReturnedActions 

switch (a.type) {
  case 'foo': 
    payload.c // error
}

We are aware that we can manually define the types using ReturnType for each method:

var actions = new Actions(); 
type = ReturnType<typeof actions.someAction1> |  ReturnType<typeof actions.someAction2>

But handling this manually in a lengthy class may not be ideal. Is there a way to automatically extract all possible return values from all methods in the class without having to do it manually?

The TypeScript version being used is "3.7.2"

Answer №1

A technique using a mapped type and conditional type can be applied to iterate through the properties of a class, filter methods, and extract their return types. By indexing this mapped type, you can obtain the union of all potential values within it.

type ReturnedActions<T> = {
    [P in keyof T]: T[P] extends (...a: any) => infer R ? {
        type: P,
        payload: R
    } : never
}[keyof T]

type AllActions = ReturnedActions<Actions>

Playground link

Edit If Actions only consists of functions, you can leverage the distributive property of ReturnType to retrieve the return type of all functions within Actions:

type AllActions = ReturnType<Actions[keyof Actions]>;

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

``Is there a specific scenario where the use of getInitialProps is recommended when automatically redirecting from one

Within my application, I have set up an auto-redirect from the root directory '/' to '/PageOne' with the following code: const Home = () => { const router = useRouter(); useEffect(() => { router.push('/pageone', ...

Expanding index signature in an interface

Is there a way to redefine the old index signature when trying to extend an interface with different property types? I am encountering an error when adding new properties that have a different type from the original index signature. interface A { a: num ...

An object may be null when its type is A or undefined, but we are certain it is not undefined

Since the release of version 4.8.4, the TypeScript compiler has been flagging an issue with the following code: type A = {v: number} function get_the_first<T>(xs: T[]): T | undefined { if (xs.length > 1) return xs[0]; else ...

How can a div be displayed next to another div when it is clicked using angular2?

Is there a way to have another page show up after clicking on a div, like shown in the image below? Before Click: https://i.sstatic.net/qSBbc.png After Click: https://i.sstatic.net/julH4.png ...

What is the best way to choose checkboxes from data that is passed dynamically?

https://i.stack.imgur.com/L3k59.png I am looking to add an edit feature to my application. When the user clicks on the edit option, they should be taken to a different page with the previously entered value displayed. While I have successfully retrieved ...

Tips for implementing UI properties in React

Utilizing an external UI Library, I have access to a Button component within that library. My goal is to create a customized NewButton component that not only inherits all props from the library Button component but also allows for additional props such as ...

I am interested in utilizing the request-reply pattern with KafkaJS in a TypeScript environment. Could you provide guidance on how to successfully implement this?

I am new to Kafka and I am trying to implement the request-reply pattern using kafkajs in TypeScript. However, my current implementation is very basic and the consumers inside producers are taking too long to start, causing delays. Is there a better way to ...

Yet another error: TS2511 - Unable to instantiate an abstract class

My issue is very similar to the one mentioned in this thread: Typescript: instance of an abstract class, however, there are some distinctions. If it is indeed a duplicate problem, I would appreciate a clear explanation as I am currently unable to resolve t ...

Utilizing formData.append in TypeScript to handle arrays

Hey there! I'm facing an issue while trying to send a form to my Profile endpoint. The problem lies in the 'user:{}' field, as I am unable to properly insert my array data into this specific field. Here is a breakdown of the fields within m ...

Highchart in ionic 2 not displaying

https://i.sstatic.net/q2CPR.png I inserted code for a highchart on my webpage, but it's not appearing I followed instructions from this video tutorial https://www.youtube.com/watch?v=FSg8n5_uaWs Can anyone help me troubleshoot this issue? This is ...

Design a data structure that encompasses the combined output of multiple functions

I have a set of functions and I want to combine the return types of these functions into a union type. Example Input function types: type InputType1 = () => {type: "INCREASE"} type InputType2 = () => {type: "ADD", by: number} Ou ...

Atom-typescript does not always successfully compile all typescript files to JavaScript

Currently, I am in the process of learning how to implement routing in Angular2 by following a tutorial. The tutorial involves creating partial pages using .ts files along with companion .js files for each page. While my Atom editor, equipped with atom-typ ...

What is the best way to programmatically click on an element within the body of a webpage from an Angular component?

I am running a crisp chat service on my website and I am attempting to interact with the chat box from one of my component's typescript files. The chat box is represented as a div element with the id crisp-client inside the body tag. Can someone plea ...

Error message displayed in console due to AJV JSON schema validation when the maximum character limit is exceeded

In this provided example: { "default": "adsds", "max": 1 } I attempted to reference the dynamically provided 'max' value and validate the number of characters entered in the 'default' field. To achieve ...

Javascript Library Issue: "Implicitly Declared Type 'Any' Error"

I am currently in the process of developing a JavaScript library that will interact with an API. My goal is to create a module that can be easily published on npm and utilized across various frameworks such as Angular or React. Below is the code snippet fo ...

Dealing with errors from APIs in a React TypeScript application

Currently, I am in the process of learning React and Typescript by creating a demo application. This app sends a request to the API located at in order to retrieve postcode information and display details about a specific location based on the entered pos ...

Unfortunately, an exception was encountered: ES Module must be loaded using the import statement

Currently, I am addressing some vulnerability concerns within my Angular development environment. These vulnerabilities are found within internal dependencies, so I have included resolutions in the package.json file. However, when I attempt to run 'ng ...

Executing methods sequentially in the ngOnInit lifecycle hook consecutively

Working with Angular15 has presented me with a challenge. In my app.component.ts file, I have two methods: “ngOnInit()”, as shown below. public ngOnInit(): void { this.getToken(); this.UserLoggedIn(); } I am looking to run the above two functions in ...

Troubleshooting issues with accessing the _id property using Typescript in an Angular application

Encountering an Angular error that states: src/app/components/employee/employee.component.html:67:92 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is ...

What is the best way to open and view files in an NPM dependency that do not use JavaScript?

I am facing an issue with an NPM project setup where my-config is a dependency of my-api. In the my-config project, there is a line of code that fetches the aws-config.ini file from the etc folder: instance.configs.aws = ini.parse(fs.readFileSync('./ ...