4

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[]'.

0

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

5 Comments

The delete idea isn't exactly the same, as it will remove the key products entirely, rather than setting it to undefined. So this.products will evaluate to undefined, but for a different reason.
Probably I haven't express myself correctly but delete this.products is what I need because if I use products: IProduct[] | undefined I can't use this.products.splice(rowNumber, 1);.
@TomFenech you're totally correct; products key will not be on Object.keys(container) if you delete it. that said; an argument could be make that if you want the key, but no value you should be setting it to null.
@VansFannel you can use array methods, you just need to check whether it's an array first. I updated my answer.
Better to set strictNullChecks to false as you say, instead of using hacky undefined as type. I prefer using null instead of undefined, but I guess that is only opinion based.
1

undefined is its own type, so you would need to change your declaration to:

public products: IProduct[]?; // optional property
// or
public products: IProduct[] | undefined; // explicit intersection with undefined 

Now that products isn't necessarily an array, you need to use a type guard before treating it as an array. We don't need to write our own, as we can use instanceof Array, which is recognised by TypeScript as a type guard:

if (this.products instanceof Array) {
    // use this.products as an array
}

Alternatively, you can set strictNullChecks to false (either by removing the compiler switch --strictNullChecks, or in your tsconfig.json), so that null or undefined are permitted values of every type. Personally I would recommend against doing this, and trying to handle possible nulls and undefineds on a case-by-case basis.

Comments

0

Just don't assign any value to it and it will remain undefined.

public products: IProduct[];

1 Comment

Thanks but I want to set it undefined when it has a value assigned.

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.