3

When declaring this :

public isCollapsedDet : boolean[][];
public isCollapsedCyc : boolean[] ;

I got the following error message :

/nestedForm/src/app/app.component.ts (95,7): Type 'boolean' is not assignable to type 'boolean[][]'.

I just need to get array as the following :

isCollapsedCyc[0] = true;
isCollapsedCyc[1] = false;
//
isCollapsedDet[0, 0] = true;
isCollapsedDet[0, 1] = true;
isCollapsedDet[1, 0] = false;
isCollapsedDet[1, 1] = true;
K Oul
  • 245
  • 1
  • 5
  • 12
  • i can only do `isCollapsedDet[1][1] = true` just set `isCollapsedDet : boolean[]` and each item `isCollapsedDet[i] = []` – Felix Jun 20 '17 at 10:07
  • 1
    You might inspire yourself from answers to this question: https://stackoverflow.com/questions/38875051/declare-an-array-in-typescript – Dawid Zbiński Jun 20 '17 at 10:10

3 Answers3

3

You cannot add values to an array by nesting them with comma. Type boolean[][] means that there will be an array of arrays of booleans, so something like for example:

[[true, false], [false, true]] // this is boolean[][] or Array<Array<boolean>>

if you want to set the value for it, you need to nest it as an ordinary array:

isCollapsedDet[0, 0] = true; 
    // error - comma has nothing to do there
isCollapsedDet[0][0] = true; 
    // success - element isCollapsedDet[0][0] in array isCollapsedDet[0] is true

More about arrays in TypeScript can be found here - and a bit more advanced types here

Some useful answers found here: Multidimensional array initialization

Other links: TypeScript Multidimensional Arrays

Dawid Zbiński
  • 5,521
  • 8
  • 43
  • 70
1

If you really only need the elements you mentioned, you could do:

let isCollapsedDet: boolean[][] = [[], []];
let isCollapsedCyc: boolean[] = [];

isCollapsedCyc[0] = true;
isCollapsedCyc[1] = false;

isCollapsedDet[0][0] = true;
isCollapsedDet[0][1] = true;
isCollapsedDet[1][0] = false;
isCollapsedDet[1][1] = true;

Or simply:

let isCollapsedDet: boolean[][] = [
    [true, true], [false, true]
];

let isCollapsedCyc: boolean[] = [true, false];

which can be reduced further because the compiler will infer the types based on the initialization:

let isCollapsedDet = [
    [true, true], [false, false]
];

let isCollapsedCyc = [true, false];
Frank Modica
  • 10,238
  • 3
  • 23
  • 39
1

When you are accessing a property in any class and if you want to make it as a class member then don't forget to mention this and as Dawid said you can't assign values by separating indexes with comma(,)

export class HelloWorld implements OnInit{

  // Declaring the variable for binding with initial value
  yourName: string = '';
  public isCollapsedDet : boolean[][] = [[], []];
  isCollapsedCyc : boolean[] = [];

  ngOnInit() {
    this.isCollapsedCyc[0] = true;
    this.isCollapsedCyc[1] = false;
    //
    this.isCollapsedDet[0][0] = true;
    this.isCollapsedDet[0][1] = true;
    this.isCollapsedDet[1][0] = false;
    this.isCollapsedDet[1][1] = true;
  }
}
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62