Mapping a Tuple to a different Tuple type in Typescript 3.0: Step-by-step guide

I am working with a tuple of Maybe types:

class Maybe<T>{ }

type MaybeTuple = [Maybe<string>, Maybe<number>, Maybe<boolean>];

and my goal is to convert this into a tuple of actual types:

type TupleIWant = [string, number, boolean];

So, I attempted the following approach:

type ExtractTypes<T> = T extends Maybe<infer MaybeTypes>[] ? MaybeTypes : never;

type TypesArray = ExtractTypes<MaybeTuple>; // string | number | boolean NOT [string, number, boolean]

Unfortunately, this solution did not yield the desired outcome.

Instead of getting the tuple [string, number, boolean], I ended up with (string | number | boolean)[]

Is it currently possible to achieve what I intended to do?

Answer №1

To tackle this issue, leverage a mapped tuple type, which gains support in TypeScript 3.1.

Crafting a mapped type with properties such as 0, 1, 2, and length of precise types is feasible, as demonstrated below:

class Maybe<T> {}

type MaybeTuple = [Maybe<string>, Maybe<number>, Maybe<boolean>];

type MaybeType<T> = T extends Maybe<infer MaybeType> ? MaybeType : never;
type MaybeTypes<Tuple extends [...any[]]> = {
  [Index in keyof Tuple]: MaybeType<Tuple[Index]>;
} & {length: Tuple['length']};

let extractedTypes: MaybeTypes<MaybeTuple> = ['hello', 3, true];

If your TypeScript version is outdated, consider using an unreleased version or resort to creating a conditional type that aligns with expected tuples numbers, like the one showcased here.

Answer №2

To avoid inadvertently mapping over unnecessary keys like push, length, etc., it is important to exclude the non-index keys of the array:

type NumStrDate = [number, string, Date]
type IdxOf<T extends any[]> = Exclude<keyof T, keyof any[]>
type OrUndef<T extends any[]> = {[K in IdxOf<T>]: T[K] | undefined}
type MaybeNSD = OrUndef<NumStrDate>

const fine: MaybeNSD = [5, 'foo', undefined]
const problem: MaybeNSD = ['foo', undefined, undefined]

If you mouse over MaybeNSD, it will display the resolved interpretation:

type MaybeNSD = {
    0: number | undefined;
    1: string | undefined;
    2: Date | undefined;
}

playground link

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

Error encountered in Typescript while attempting to $set a subdocument key value in MongoDB

This is a sample data entry. { _id: ObjectId('63e501cc2054071132171098'), name: 'Ricky', discriminator: 7706, registerTime: ISODate('2023-02-09T14:23:08.159Z'), friends: { '63e502f4e196ec7c04c4 ...

tips for sending types as properties in a versatile component

I am facing a challenge with passing types as props to a reusable component. I have a component where I pass rows and headings as props, but I need to find a way to pass types as props as well. Below is the code for my reusable component: import { TableCe ...

Modify the property of the ChildComponent by utilizing the ViewChild method

I recently started exploring Angular and I've been experimenting with ViewChild and ViewChildren. In one scenario, I have a property called today = new Date() in my Component2. I'm accessing this property in Component1 using ViewChild and continu ...

Module 'next-intl/client' cannot be located

When I run npm test, I encounter the following error: 'next-intl/client' module not found jest.mock( | ^ 22 | 'next-intl/client', 23 | (): Record<string, unknown> => ({ 24 | usePathname: ...

Determination of the input parameters

Currently, I am developing an Angular application that includes a matInput field for user input. The issue I am encountering is that when the user enters a positive or negative value in the field (e.g. +5 or -5), the final output does not reflect the inten ...

Launching Nest.js application from Visual Studio Code

Currently experimenting with a new framework called which seems promising as it integrates TypeScript into Node, similar to Angular. I'm using the starter template from https://github.com/kamilmysliwiec/nest-typescript-starter. I can start it withou ...

Multiple asynchronous calls in Angular 2

In my Component, there is a function that is supposed to return a value (Promise). This value requires information from two distinct sources: an API call and data from a database. The method in question looks like this: public getValue(): Promise<numb ...

What is the best way to test chained function calls using sinon?

Here is the code I am currently testing: obj.getTimeSent().getTime(); In this snippet, obj.getTimeSent() returns a Date object, followed by calling the getTime() method on that Date. My attempt to stub this functionality looked like this: const timeStu ...

Managing empty functions as properties of an object in a React, Redux, and Typescript environment

I'm feeling a little uncertain about how to properly test my file when using an object with a function that returns void. Below are the details. type Pros={ studentid: StudentId pageId?: PageID closeForm: () => void } When it comes to creating ...

Detecting the language of a browser

In my Angular2 application, I am looking to identify the browser language and use that information to send a request to the backend REST API with localization settings and variable IDs that require translation. Once the request is processed, I will receive ...

What is the best way to convert the reader.result into a string?

Whenever I attempt to upload an image on Angular, I encounter an error with reader.result in the TypeScript file below. How can I resolve this issue? I even included console.log(image) in the onImagePicked function but it doesn't display anything in ...

Attempting to perform an API invocation on a distant endpoint utilizing NestJS

var unirest = require("unirest"); var req = unirest("GET", "https://edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data"); req.query({ "ingr": "1 large apple" }); req.headers({ &qu ...

Is it possible for a component to have multiple templates that can be switched based on a parameter?

Scenario My task is to develop a component that fetches content from a database and displays it on the page. There needs to be two components working together to create a single page, following a specific component tree structure. DataList - receives ful ...

Can you provide a guide on setting up and utilizing mathlive within NuxtJS?

Can anyone assist me? I am trying to figure out why my code is not working or if I have implemented it incorrectly. I used npm i mathlive to obtain input. However, following the instructions for implementing nuxt plugins in the documentation has not yield ...

What is the reason behind RematchDispatch returning a `never` type when a reducer and an effect share the same name?

Recently, I made the switch from TypeScript 4.1.2 to 4.3.2 with Rematch integration. Here are the Rematch packages in use: "@rematch/core": "2.0.1" "@rematch/select": "3.0.1" After the upgrade, a TypeScript error p ...

Tips on avoiding updates to a defined object when a new object is filtered (created from the original object)

Is there a way to filter an array of objects based on their year without altering the original object? Whenever I apply a filter, it affects both the newly created object and the original one. However, I need the original object to remain unchanged so that ...

The TypeScript compiler is attempting to fetch node modules in the browser while compiling a single output file

After setting up my tsconfig file to build a frontend typescript application with a single output structure, I encountered an unexpected issue: { "compilerOptions": { "target": "es5", "module": "system", "lib": [ "e ...

Exploring the latest upgrades in React 18 with a focus on TypeScript integration

I am currently working on a complex TypeScript project with React and recently made the decision to upgrade to the new version of React 18. After running the following commands: npm install react@18 npm install react-dom@18 npm install @types/react-dom@18 ...

Converting JSON to TypeScript with Angular Casting

I'm facing an issue detailed below. API: "Data": [ { "Id": 90110, "Name": "John", "Surname": "Doe", "Email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="472d282f2923282207202a262e2b ...

Convert image file to a React TypeScript module for export

Imagine a scenario where you have a folder containing an image file and an index file serving as a public API. Is there a method to rename the image file before reexporting it? Here is the structure of the folder: └── assets/ ├── index.t ...