简体   繁体   中英

Typescript interfaces

I am trying to create an interface for the following object in angular 2:

// set the render state object
    this.aRenderState = {
      model: "",
      colour: false,
      showWireframe: false,
      showGrid: true,
      clipping: {
          enabled: false,
          visible: false,
          planes: [
              {
                  name: 'X',
                  plane: new THREE.Plane(new THREE.Vector3(1, 0, 0), 0),
                  object: null,
                  enabled: true,
                  flip: false,
                  size: [this.aDomain.getSize().z, this.aDomain.getSize().y],
              },
              {
                  name: 'Y',
                  plane: new THREE.Plane(new THREE.Vector3(0, 1, 0), 0),
                  object: null,
                  enabled: false,
                  flip: false,
                  size: [this.aDomain.getSize().x, this.aDomain.getSize().z]
              },
              {
                  name: 'Z',
                  plane: new THREE.Plane(new THREE.Vector3(0, 0, -1), 0),
                  object: null,
                  enabled: false,
                  flip: false,
                  size: [this.aDomain.getSize().x, this.aDomain.getSize().y]
              },
          ]
      }
    }

the following is my attempt:

export interface IPlane {
  name: string;
  plane: THREE.Plane;
  object: {};
  enabled: boolean;
  flip: boolean;
  size: number[];
}

export interface IClipping {
  enabled: boolean;
  visible: boolean;
  planes: IPlane[];
}

export interface IRenderState {
  model: string;
  colour: boolean;
  showWireframe: boolean;
  showGrid: boolean;
  clipping: IClipping;
}

when i run the project I get the following error message:

Type '{ model: string; colour: false; showWireframe: false; showGrid: true; clipping: { enabled: false;...' is not assignable to type 'IRenderState'. Types of property 'clipping' are incompatible. Type '{ enabled: false; visible: false; planes: [{ name: string; plane: Plane; object: null; enabled: t...' is not assignable to type '{ enabled: boolean; visible: boolean; planes: [IPlane, IPlane, IPlane]; }'. Types of property 'planes' are incompatible. Type '[{ name: string; plane: Plane; object: null; enabled: true; flip: false; size: [number, number]; ...' is not assignable to type '[IPlane, IPlane, IPlane]'. Type '{ name: string; plane: Plane; object: null; enabled: true; flip: false; size: [number, number]; }' is not assignable to type 'IPlane'. Types of property 'size' are incompatible. Type '[number, number]' is not assignable to type '[0, 0]'. Type 'number' is not assignable to type '0'.)

I cannot understand what I am doing wrong.

Typescript 2.2 Has "object" type (all lower). It is recommended to use this one from now.

interface IPlane {
    object: object;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM