Issue
I'm trying to create a TS User class with angular. I declared my attributes as shown in the code below, but I'm getting this error.
An argument for 'id' was not provided.
export class User {
private id: number;
private etabName: string;
constructor(id: number, etabName: string, ) {
this.id = id;
this.etabName = etabName;
}
get _id(): number {
return this. id;
}
set _id(value: number) {
this. id = value;
}
}
Solution
The issue occurs when you try to instantiate the class without passing arguments, as you did not specify arguments will be optional.
Make arguments optional like this
export class User {
...
constructor(id?: number, etabName?: string, ) {
this.id = id || 0; //Specify default value
this.etabName = etabName || ''; //Specify default value
}
...
}
then you can instantiate class without arguments
const user = new User();//works
Answered By - Sameer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.