What is the method for creating a generic type that permits the setting of partial defaults for a specified type?

I am struggling with generating correct types for the genStruct function. Although I know how to make the function work, creating the right types has been challenging for me.

Below is the code snippet of my function:

type Breakpoint = "sm" | "md" | "lg"
export const genStruct = <T extends object>(
    defaultValue: T,
    breakpoints: Breakpoint[],
): Partial<Responsive<T>> => { // Using Partial to allow omission of keys
    return breakpoints.reduce((acc, breakpoint) => {
        acc[breakpoint] = Object.keys(defaultValue).length === 0
            ? ('' as any)
            : { ...defaultValue }
        return acc
    }, {} as Partial<Responsive<T>>)
}
type NestedProps = {
    rows: string
    cols: string
}
type Responsive<T> = {
    [key in Breakpoint]: T
}
type Default = Responsive<NestedProps>
const some_value: Default = {
    sm: {
        rows: '',
        cols: '',
    },
    ...genStruct({
        rows: '',
        cols: '',
    }, ['md', 'lg'])
}

The TypeScript error message I'm receiving is as follows:

Type '{ sm: { rows: string; cols: string; }; md?: { rows: string; cols: string; } | undefined; lg?: { rows: string; cols: string; } | undefined; }' is not assignable to type 'Default'. Types of property 'md' are incompatible. Type '{ rows: string; cols: string; } | undefined' is not assignable to type 'NestedProps'. Type 'undefined' is not assignable to type 'NestedProps'.

Answer №1

After some careful consideration, I believe I have found the answer.

export const genStruct = <T extends object, K extends Breakpoint>(
    defaultValue: T,
    breakpoints: K[],
): { [P in K]: T } => {
    return breakpoints.reduce((acc, breakpoint) => {
        acc[breakpoint] = { ...defaultValue };
        return acc;
    }, {} as { [P in K]: T });
};

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

I want to know the proper way of utilizing ionViewDidLoad() within a component

Greetings! I am currently delving into ionic 3 and have some data stored in local storage. My aim is to utilize this data within a component. However, it seems that ionViewDidLoad, which functions perfectly on a regular page, is not working on a component. ...

Eslint requires that return values are used effectively

I am seeking a TypeScript or ESLint rule that enforces the usage of return values from functions. For example: const getNum = () => 12 This rule should allow me to call the function and store the returned value like this: const a = getNum() However, i ...

What methods could I utilize to implement an observable on a checkbox to check its status and subsequently refresh my application page?

Hello, I have a question. I am trying to implement a feature in my application where the page automatically reloads every 1 minute if a checkbox is selected. If the checkbox is not selected, the automatic page update should be disabled. I have been attempt ...

Ways to verify the existence of a property if obj is one of the specified types

Suppose I have a few interfaces A, B, C that implement a common Base. interface Base { x: number; y: number; z: number; } interface A extends Base { a: true; } interface B extends Base { b: true; } interface C extends Base { c: t ...

What is the minimum length requirement for a text box in Ionic 2 with Angular 2?

I've been successfully validating a textbox with angular2, and now I'm attempting to set a minimum character limit for the input. However, when I try to do this, I encounter the following error: Expected validator to return a Promise or Observab ...

FilterService of PrimeNg

Looking for assistance with customizing a property of the p-columnFilter component. I have managed to modify the filter modes and customize the names, but I am having trouble with the no-filter option. Has anyone found a solution for this? this.matchMo ...

Retrieve a specific data point from a web API using Angular framework

Objective: How can I retrieve the specific value "test" in Angular? Issue: An error message is being displayed. Error: SyntaxError: Unexpected token e in JSON at position 1 at JSON.parse () Which syntax element am I missing? ASP.NET // Retrieve "tes ...

Discovering the 3D coordinates of a point that is perpendicular to the midpoint of a line

(I am working with Javascript/Typescript and Three.js) Given two vectors, let's say {x:1, y:3, z:5} and {x:7, y:8, z:10}, I have a direct straight line connecting them. At the midpoint of this line, envision a disc with a radius of 1 that is perpend ...

Adding JSON content to a form for editing functionality within an Angular 8 CRUD application

I am currently working on building a Single Page Application using Angular 8 for the frontend and Laravel for the backend. I have been able to successfully pass data to the backend via JWT, and everything is functioning as expected. The application follows ...

"Is there a way to dynamically remap an array only when there are changes

One of the challenges I am facing is with a component called Page, which contains two components - Editor and Preview. Page has an array called items. [ { value: 0, text: 'Item 1' }, ... ] This array items is passed ...

Creating the ideal Typescript Interface for a Firebase Response JSON: Best Practices

I am receiving a JSON response from Firebase with the following format: { "-KD8Evk7TULU6t6zxMHl": { "createdAt": 1458296568840, "isActive": true, "title": "...add a title", "updatedAt": 1458296568840 } } Question Part One: How should I define my Typescri ...

Executing an animation in Angular 4 using a Directive

There's an ongoing issue on the repository here, but I wanted to see if anyone here could help as well. I am trying to programmatically trigger an animation from a Directive. However, when using Renderer.animate, I receive the following error: Rende ...

Converting data types of a destructured property

In my Next.js application, I'm using a router hook and destructuring like this: const { query: { run_id }, } = useRouter(); The type of `run_id` is as follows: run_id: string | string[] | undefined I tried to typecast it as shown below, but it doe ...

Can a class method be utilized within a Module without being shared with other modules in Angular and TypeScript?

Here is a scenario to consider export class X{ y():void{} z():void{} } I am currently in Module N I need to utilize method y() and z() within module N, but I want to restrict access to method y() from other modules while still allowing acces ...

Attempting to create a sorting functionality using Vue and Typescript

I am trying to implement a feature where clicking on the TableHead should toggle between sorting by most stock on top and least stock on top. Currently, I have two buttons for this functionality, but it's not very user-friendly. My approach involves ...

Discover the dynamic method for determining the value of a nested array based on a specific condition

Currently, I am delving into the world of nested arrays and attempting to locate a specific value within the array based on a condition. The data returned by the API is structured in the following format: data= { "ch_Id": "1234", "title": "Title" ...

How can I display options in a react autocomplete feature?

Using the autocomplete component from @material-ui/lab/autocomplete, I am trying to retrieve the title_display result in the options field. These results are being fetched from an API using axios. You can view my code here--> https://codesandbox.io/s/r ...

Using Typescript and ThreeJS, include new elements to the environment within the loader

Can someone help me with the following code snippet? export class LandingPageComponent implements OnInit { scene: THREE.Scene; (...) ngOnInit() { this.scene = new THREE.Scene(); var loader = new THREE.JSONLoader(); loader.load("../../assets/fire_lion.j ...

Is there a way to add zeros at the beginning of ZIP codes that have only 3 or 4 digits using LODASH or Typescript?

Looking at the JSON data below, it includes information on USPS cities, states, counties, latitude, longitude, and zip codes. With over 349,000 lines of data, it's very extensive. ... { "zip_code": 988, "latitude": 18.39 ...

Calculating the total of all values in a table

For my ngFor loop, the invoice total is calculated based on price and hours, but I also want to calculate the totals of all invoices in the end. <tr *ngFor="let invoice of invoiceItem.rows"> <td>{{ invoice.rowName }}</td> <td& ...