Is there a way to define a return type conditionally depending on an input parameter in typing?

I need help ensuring that a function in TypeScript returns a specific type based on a parameter. How can I make TypeScript understand my intention in this scenario?

type X = 'x'
type Y = 'y'

const customFunc = <Type extends X | Y>(input: Type): Type extends X ? 'result_x': 'result_y' => {
    if (input === 'x') return 'result_x'
    
    return 'result_y'
}

// The output type here will be 'result_x'
const outputX = customFunc('x')
// The output type here will be 'result_y'
const outputY = customFunc('y')

Playground Link

Answer №1

Utilizing Function Overloads is typically recommended for handling such situations, but it is also feasible to engage a custom type map that encompasses all potential return types based on the Union Type parameter.

type X = 'x';
type Y = 'y';

type OutputMap = {
  [M in X]: 'x1';
} & {
  [N in Y]: 'y1';
};

function customFunction<U extends X | Y>(input: U): OutputMap[U] {
  const outputMap: OutputMap = {
    x: 'x1',
    y: 'y1',
  };

  if (input in outputMap) {
    return outputMap[input];
  }

  throw new Error('Invalid input');
}

const x1 = customFunction('x'); // 'x1'
const y1 = customFunction('y'); // 'y1'

TypeScript 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

Accessing the ViewModel property of a parent component from the ViewModel of its child in Aurelia

Having a scenario with two distinct components: <parent-component type="permanent"> <div child-component></div> </parent-component> class ParentComponentCustomElement { @bindable public type: string = "permanent"; } clas ...

Error in Next.js when trying to use Firebase Cloud Messaging: ReferenceError - navigator is not defined in the Component.WindowMessagingFactory instanceFactory

Currently, I am in the process of setting up push notifications with Firebase in my next.js application. I have been following a guide from the documentation which you can find here: https://firebase.google.com/docs/cloud-messaging/js/receive?hl=es-419 Ho ...

Angular 2.0 encountered an unexpected value from the module 'AppModule' which appears to be an '[object Object]'

Every time I attempt to load my angular version 2.0 application, I encounter the following error: (index):21 Error: Error: Unexpected value '[object Object]' imported by the module 'AppModule' import { ModuleWithProviders } from ' ...

Prisma auto-generating types that were not declared in my code

When working with a many-to-many relationship between Post and Upload in Prisma, I encountered an issue where Prisma was assigning the type 'never' to upload.posts. This prevented me from querying the relationship I needed. It seems unclear why P ...

Difficulty encountered when attempting to invoke a public function that makes use of a private function in TypeScript

I'm relatively new to TypeScript and I've been trying to replicate a pattern I used in JavaScript where I would expose functions through a single object within a module (like "services"). Despite my efforts, I'm facing some issues when attem ...

The initial processing step for the export namespace is to utilize the `@babel/plugin-proposal-export-namespace-from` plugin. Previous attempts to resolve this issue have been

I encountered a problem with my application and found two related questions on the same topic. However, due to a lack of reputation, I am unable to comment or ask questions there. That's why I'm reaching out here... Recently, I integrated Redux ...

Tips for retrieving both the ID and NAME values from Ionic's Dynamic Select Options

In my Ionic 3 project, I have successfully implemented a dynamic select option. However, I am facing an issue where I can only retrieve either the ID or the Name value of the selected option from the server, but not both. I have tried using JSON.parse and ...

What causes TypeScript to automatically infer a default property when dynamically importing a JavaScript file that lacks a default export?

While dynamically importing a javascript file that exports multiple functions (without a default export), I encountered this issue: const sayHi = import('./sayHi.js') I was expecting the type of sayHi to be Promise<{name1: function, name2: fu ...

Is it possible to set the initial value of useState() as null and later assign it an object value?

https://i.sstatic.net/TjAbz.png Looking at the image, I am attempting to set up a React state hook for the alert system on my website. Initially, I want no alerts to be displayed. However, when a user clicks a button, I want to show a success alert with a ...

What causes Next.js to struggle with recognizing TypeScript code in .tsx and .ts files?

Webpages lacking a declared interface load correctly https://i.stack.imgur.com/DJZhy.png https://i.stack.imgur.com/r1XhE.png https://i.stack.imgur.com/zXLqz.png https://i.stack.imgur.com/Z1P3o.png ...

Encountering a Typescript error while attempting to utilize mongoose functions

An example of a User interface is shown below: import {Document} from "mongoose"; export interface IUser extends Document{ email: string; password: string; strategy: string; userId: string; isValidPassword(password: string): ...

Discovering the World of React with Typescript: Implementing Flexible Routes with BrowserRouter

When navigating to http://localhost:3000/confirm_email/, the route loads correctly. However, if I navigate to http://localhost:3000/confirm_email/h8s03kdbx73itls874yfhd where h8s03kdbx73itls874yfhd is unique for each user, I still want to load the /confirm ...

TypeScript is unable to recognize files with the extension *.vue

Can someone assist me with an issue I'm facing in Vue where it's not detecting my Single File Components? Error message: ERROR in ./src/App.vue (./node_modules/ts-loader!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/Ap ...

Unable to retrieve information from a Node.js server using Angular 2

I am currently learning Angular 2 and attempting to retrieve data from a Node server using Angular 2 services. In my Angular component, I have a button. Upon clicking this button, the Angular service should send a request to the server and store the respo ...

Is it possible for an uninitialized field of a non-null literal string type to remain undefined even with strict null checks in

It seems that there might be a bug in Typescript regarding the behavior described below. I have submitted an issue on GitHub to address this problem, and you can find it at this link. The code example provided in that issue explains the situation more clea ...

You must provide a secret or key in order to use the JwtStrategy

I have encountered the following error and I am unsure of its cause. Can you assist me? ERROR [ExceptionHandler] JwtStrategy requires a secret or key TypeError: JwtStrategy requires a secret or key at new JwtStrategy (C:\Users\wapg2\OneDriv ...

The task of mapping an array of objects with nested values using JavaScript is proving to

Attempting to convert an array of objects with nested values in child objects like: const objs = [{ "B": { "value": 1, }, "D": { "value": "45" }, "E": { "value": "234" }, ...

Obtain precise measurements of a modified image using the Sharp library

My Cloud Function successfully resizes images uploaded to Cloud Storage using Sharp. However, I am facing an issue with extracting metadata such as the exact height and width of the new image. I am contemplating creating a new function that utilizes diff ...

Issues with implementing PrimeNG Data List component in Angular 4

I'm encountering an issue with my data list in primeng as I try to run the app: Can't bind to 'value' since it isn't a known property of 'p-dataList' The HTML file looks like this: <p-dataList [value]="cars"> ...

Searching for particular information within an array of objects

Seeking guidance as a newbie on how to extract a specific object from an array. Here is an example of the Array I am dealing with: data { "orderid": 5, "orderdate": "testurl.com", "username": "chris", "email": "", "userinfo": [ ...