BananaAcid
BananaAcid

Reputation: 3481

How to declare a new complicated variable type

I am trying to define a rather complicated array and a function to return different constants. My pseudo code looks like this:

// file: matrix.d.ts (to accompany matrix.js)
declare module 'matrix' {

  // values restricted to ...
  type MATRIX_VALUE = (null|-1|0|1);
                       ^ ERROR: [ts] Type expected.
  // OR:
  type MATRIX_VALUE = Number?;
                            ^ ERROR: [ts] ';' expected.

  // array length restricted to 3 in 3, + specific values
  type Matrix = Array<Array<MATRIX_VALUE>[3]>[3];
                                          ^ ERROR: [ts] ']' expected.
  // OR:
  type Matrix = Array<Array<MATRIX_VALUE>(3)>(3);
                                         ^ ERROR: [ts] '>' expected.

  export const A;
  export const B;

  export function testMatrix(mat: Matrix): A|B;  
                                           ^ ERROR: [ts] can not find name 'A'.

}

I am pretty lost. There seems to be no documentation on it. Any ideas? Maybe I am using it completely the wrong way. I am grateful for any direction on solving this.

Upvotes: 0

Views: 85

Answers (1)

basarat
basarat

Reputation: 276249

type MATRIX_VALUE = Number

With TypeScript latest that supports null types:

type MATRIX_VALUE = number | null

array length restricted to 3 in 3,

Use a tupple type

type MatrixRow = [number,number,number]
type Matrix = [MatrixRow,MatrixRow,MatrixRow]

Upvotes: 1

Related Questions