1

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)
  }
3
  • Can you include the this.dataPipe code? TypeScript is complaining that that might return null, and then you can't pass that into setDate. Commented Jun 16, 2021 at 15:27
  • it's a pipe library from angular , i declare it in the constructor ' private datePipe: DatePipe ' Commented Jun 16, 2021 at 15:30
  • 1
    You just have to handle the possible null value coming out of datePipe.transform. WIth an if for instance Commented Jun 16, 2021 at 15:34

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also might be in your case this.datePipe.transform is returning null, you will need to handle that
0

try change your datatypes to lowercase

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

1 Comment

still nothing change
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.