stack kkok
stack kkok

Reputation: 17

Argument of type 'string | null' is not assignable to parameter of type 'String'. Type 'null' is not assignable to type 'String'. [angular]

im trying to affect a set Attribute from class.ts with a variable in my component.ts ( angular ) like this :

this.UserPaiemet.setDate(this.DateN)

The IDE shows me this errors :

Argument of type 'string | null' is not assignable to parameter of type 'String'.
  Type 'null' is not assignable to type 'String'.

131     this.UserPaiemet.setDate(this.DateN)

i don't know how can i fix it .

My class:

export class UserPaiemet
{
 private id!:Number;
 private date!:String;
 private nbrmois!:Number;
}

ANd my component Method

date = new Date()
  DateN  = this.datePipe.transform(this.date, "yyyy-MM-dd")
  UserPaiemet= new UserPaiemet()
  updateUserPai()
  {
  this.UserPaiemet.setDate(this.DateN)
  }

Upvotes: 0

Views: 2159

Answers (3)

Pritam Rajput
Pritam Rajput

Reputation: 118

Please use the following convention for declaring types eg.(String -> string)

export class UserPaiemet {
    private id!: number;
    private date!: string;
    private nbrmois!: number;
}

date = new Date();
DateN = this.datePipe.transform(this.date, "yyyy-MM-dd") || ''; // or dummy date here
UserPaiemet = new UserPaiemet();
updateUserPai();
{
    this.UserPaiemet.setDate(this.DateN);
}

Upvotes: 2

Invictus
Invictus

Reputation: 546

In javascript "number" is the primitive type, "Number" instead is the object type. As a comparison in Java int and Integer (int is primitive type Integer instead is object type) so in your case Number is not equal to number. But in your case the problem is another: you cannot assign null or string to String type. Probably the ide is not recognizing well the object type so try to put it in lower case and execute it.

otherwise try it:

export class UserPaiemet
{
 private id:number;
 private date:string|null;
 private nbrmois:number;
}

Upvotes: 0

Lautaro Lopez Ruiz
Lautaro Lopez Ruiz

Reputation: 144

try change your datatypes to lowercase

export class UserPaiemet
{
 private id:number;
 private date:string;
 private nbrmois:number;
}

Upvotes: 0

Related Questions