Create an interface that consists of both mandatory and optional properties, where some properties are required in specific scenarios as shown below
export interface IUser{
name: string;
address: string;
semester?: string,
degree?: string
}
In this interface, 'name' and 'address' are compulsory for all cases, while 'semester' and 'degree' can be provided based on certain conditions.
You can utilize this interface within the constructor of your User
class to assign values to the properties like so:
export class User{
//define all fields here
public name: string;
public address: string;
public semester?: string;
public degree?: string;
constructor(user:IUser) {
this.name = user.name;
this.address = user.address;
this.semester = user.semester;
this.degree = user.degree;
}
}
To instantiate the class, you can follow the approach below -
let data2:IUser = { name: data.name, address: data.address, degree: data.degree};
let techobj = new User(data2);
Alternatively, you can simplify it like this:
let techobj2 = new User({ name: data.name, address: data.address, degree: data.degree});
For creating an instance of Student
, you can do it in the following manner -
let techobj3 = new User({ name: data.name, address: data.address, semester:data.semester });
The complete code snippet is presented below -
export interface IUser{
name: string;
address: string;
semester?: string,
degree?: string
}
export class User{
//define all fields here
public name: string;
public address: string;
public semester?: string;
public degree?: string;
constructor(user:IUser) {
this.name = user.name;
this.address = user.address;
this.semester = user.semester;
this.degree = user.degree;
}
}
let data:IUser = { name: "Niladri", address: "123", degree: "GRAD" };
let techobj = new User(data);
///OR
let techobj2 = new User({ name: "Niladri", address: "123", degree: "GRAD" });
//Similarly
let techobj3 = new User({ name: "Niladri", address: "123", semester:"6th" });
console.log(techobj2.degree); //GRAD
console.log(techobj3.semester); //6th
For service calls, structure them as demonstrated below -
selectedUser:string; //determine selected option such as teacher etc
submit(data){
if(selectedUser === 'Teacher'){
let teachobj = new User({ name: data.name, address: data.address, degree: data.degree});
this.userservice.post(url,teachobj);
}
if(selectedUser === 'Student'){
let stuobj = new User({ name: data.name, address: data.address, semester:data.semester });
this.userservice.post(url,stuobj);
}
..... continue with other conditions
}
Note: Passing null for undesired parameters in the current constructor will also function correctly.
Edit 2: Another method involves creating a base class User
and deriving sub-classes like Student
and Teacher
, then invoking super()
in the child classes' constructors to transmit the name
and address
property values to the parent User
class. However, this may result in numerous child classes depending on the number of conditionals present.
View the working demo at this link