diho472
diho472

Reputation: 109

How to solve in angular - type 'string | null' is not assignable to type 'string'

I have variables declared in the .ts file:

export interface Payment {
     paymentStatus?: string
}

In the component.ts file, I create:

filters: PaymentFilters = {
paymentStatus: null
}

In console I get:

Type null is not assigned to type string

and

type string | null is not assigned to type string.

Why do I get such an error when I declare that the variable is a string and can optionally occur?

Upvotes: 0

Views: 798

Answers (1)

Avraham Weinstein
Avraham Weinstein

Reputation: 894

Adding the optional sign ? is equivalent to string | undefined. In Javascript as well as in Typescript, undefined and null are not of the same type.

you should then declared your interface as the following:

export interface Payment {
     paymentStatus: string | null
}

Upvotes: 1

Related Questions