Home > database >  Nullable boolean in Angular
Nullable boolean in Angular

Time:10-07

I'm trying to set a array with a null in to a boolean, but I dont know how to do it, here is my code

export interface PeriodicElement {  
caja: boolean;
tractor: boolean;
}
const ELEMENT_DATA: PeriodicElement[] = [
{caja:null, tractor:false}
]

CodePudding user response:

You can use union types to achieve this.

Union Types - Typescript docs

The first way to combine types you might see is a union type. A union type is a type formed from two or more other types, representing values that may be any one of those types. We refer to each of these types as the union’s members.

export interface PeriodicElement {  
   caja: boolean | null;
   tractor: boolean | null;
}
  • Related