Create two distinct classes featuring varied decorator choices

I need to split a single class definition into two separate classes: one with nullable true, and the other with nullable false. Specifically, I'm trying to create different versions of PlanInput.

const createExample = (nullable) => {

  @InputType()
  class PlanInput {

    @IsNotEmpty({ message: 'Plan name can\'t be empty' })
    @Trim()
    @Field({ nullable })
    name?: string

  }

  return PlanInput
}

export const PlanInput = createExample(true)
export const PlanCreateInput = createExample(false)

Can this separation be achieved using TypeStack or just by utilizing ES6 features?

Answer №1

To achieve this functionality, simply extend the PlanInput class and set nullability using a private boolean variable.

@InputType()
class PlanInput {
  private nullable = true;
  @IsNotEmpty({ message: 'Plan name can\'t be empty' })
  @Trim()
  @Field({ nullable: this.nullable })
  name?: string
}

class PlanCreateInput extends PlanInput {
  private nullable = false;
}

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

choose a distinct value for every record in the table

My goal is to only change the admin status for the selected row, as shown in the images and code snippets below. When selecting 'in-progress' for the first row, I want it to update only that row's status without affecting the others. <td ...

Error: ObjectId casting unsuccessful for the input value "{ userId: '5c48a95df9bd9a33c0ff9405'"

'An issue occurred while attempting to convert the value "{ userId: '5c48a95df9bd9a33c0ff9405', username: 'ahsan', iat: 1549024353, exp: 1549110753 }" to an ObjectId for the "user" path in the "Rental" model' router.get ...

Tips on typing a collection that may hold numerous instances of a particular object

When working with NgRx actions, I need to define the parameter. This parameter is an object that can contain a varying number of specific objects. These objects are already defined in an Interface. export interface CustomDistribution { maxWindowsActive ...

When running ng build, the DefinitelyTyped package @types/async encounters issues

Previously, we built a website last year using the following versions: node v6.9.1 npm v3.10.8 angular-cli v1.0.0-beta.24 typescript v2.1.4 The application was successfully created by the previous developer. However, after setting up these versions and ...

The module "@react-native-firebase/firestore" does not have the specified exported member named 'CollectionReference'. (Error code: ts2614)

Utilizing TypeScript and React Native with Firestore (without expo). When attempting to import: import { CollectionReference } from '@react-native-firebase/firestore' An error is received: The module "@react-native-firebase/firestore" does no ...

Is there a way to integrate Angular NgRx dependent selectors and Observables in a loop without the need to unsubscribe from updates consistently?

We are currently utilizing Angular (v18) NgRx for a large application where actions are firing frequently. A new feature requires us to retrieve an array of values and then call another selector for each item in the array in order to construct a view model ...

Is it possible to easily check for a recurring situation within React functions?

In my development process for a React component, I am looking to expand the testing coverage. This particular component includes a disabled property that essentially disables all functionalities of the component. The challenge arises when I realize that I ...

What is the Correct Way to Send Functions to Custom Directives in Angular 2 Using TypeScript?

I am relatively new to Angular 2. I am currently in the process of upgrading my application from AngularJS and focusing on completing the UI/UX development. There is one final issue that I am seeking help with, and I appreciate any assistance provided. Cu ...

The npm script for running Protractor is encountering an error

Currently, I am facing an issue while trying to execute the conf.js file using an npm script. The conf.js file is generated within the JSFilesRepo/config folder after running the tsc command as I am utilizing TypeScript in conjunction with protractor-jasmi ...

Ways to display various images by clicking on the previous page

I recently started using Angular and I am facing a challenge with my 2-page setup. On the first page, there is a list of main category icons displayed. I would like to show the subcategory icons on the next page based on the selected main category. For e ...

What is the best way to bring in Angular2 components that have been compiled into one single file

Currently, I am in the process of integrating Angular2 with Laravel by following the guidance provided in the Quick Start guide. To compile all my typescript files into a consolidated app.js, I have set up elixir-typescript and executed gulp, which resulte ...

I'm struggling with deleting a row from a pushed Object in Angular 2 and could use some guidance

Let me start by explaining my current issue: Initially, the problem was like this: , where it deleted the last created Object, Conditions in my case. However, I need to be able to delete each condition separately. For example, as shown with the red box n ...

Choosing the Active Browser Tab while Modal is Open in Angular

In my current situation, I have implemented a function in the first tab that displays a modal or component after 5 seconds: ngOnInit() { setTimeout(() => { this.openDialog(); }, 5000); } openDialog() { this.dialog.open(.....); } However, if ...

What is the best way to implement a hover effect on multiple rows within an HTML table using Angular?

I am currently working on developing a table preview feature to display events. I previously sought assistance here regarding positioning elements within the table and successfully resolved that issue. Applying the same principles, I am now attempting to c ...

Error in Typescript: Issue with Object.fromEntries Typescript Error

In my TypeScript code, I have a function that utilizes Object.fromEntries to simplify a complex response object and organize it by using a substring of the child object key. let Newresult = res.map(object => Object.fromEntries(Object.entries(object).ma ...

Design system styled component - "The type of X cannot be explicitly defined without a reference..."

How can I resolve this TypeScript issue? I have a styled component exported from a style.ts file and used in the index.tsx file of my React component: style.ts: import { styled, Theme } from '@mui/material/styles'; type CardProps = { theme? ...

What is the recommended way to include @types/module in a TypeScript project?

Once I've added a module like @types/express using npm, how can I correctly reference it in typescript? I've tried the following methods: /// <reference path="../node_modules/@types/express/index.d.ts" /> but I still get an error sayin ...

In Angular 2 Type Script service, make sure to include the @angular/core module for proper functionality as the 'require' method may not

I am encountering an issue with a service I am using. Whenever I try to start the page, I receive an error message. Here is the screenshot of the error: https://i.sstatic.net/WMzfU.png The compiled .js file contains the following code: reuired('@ang ...

What are some alternatives to using create-react-app when compiling TSX files?

Currently, I am developing a simple React application without using modules or create-react-app. My components are directly included in script tags within the HTML page. An example of one of my components, Tips.js, looks like this: class Tips extends Reac ...

Setting a value to a FormBuilder object in Angular 2 with Typescript

During my use of Reactive form Validation (Model driven validation), I encountered an issue with setting the value to the form object on a Dropdown change. Here is my Formgroup: studentModel: StudentModel; AMform: FormGroup; Name = new FormControl("", Va ...