I'm developing an Angular 2 application. This the first time I do something with Angular or Typescript.
I have this variable inside of a class:
public products: IProduct[];
IProduct
is:
interface IProduct {
productCode: string;
description: string;
lawId: number;
name: string;
comment: string;
emvoProduct?: IEmvoProduct; // ?: Optional.
}
Is there any way to set it to undefined?
When I do this:
this.products = undefined;
I get an error saying:
(TS) Type 'undefined' cannot be converted to type 'IProduct[]'.
Its because of strictNullChecks
compile option in your tsconfig.json
; you can simply remove or set to false.
Alternatively, you have to specify that the field can be undefined:
products: IProduct[] | undefined
Another alternative is to delete
delete this.products
this will truly make the field undefined
when you check for it because its simply not there. typescript will be happy with it -- even with strictNullCheck: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With