Shape' refers to a value, but is being used as a type here. Did you mean 'typeof Shape'? <ArkTSCheck>

在 ArkTS 中遇到这个错误提示,是因为代码混淆了值和类型的使用。Shape 被定义成了一个值(比如变量或者常量),但在当前代码位置,它正被当作类型来使用,而这不符合语法规则。
 

export class Shape {
  draw(): void {
    console.log("绘制图形。");
  }
}

export class Rectangle extends Shape {
  draw(): void {
    console.log("绘制矩形。");
  }
}

export class Circle extends Shape {
  draw(): void {
    console.log("绘制圆形。");
  }
}
import { Rectangle, Circle } from '../data/ShapeClass';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

let shapes: Shape[] = [];
shapes.push(new Rectangle());
shapes.push(new Circle());

for (let shape of shapes) {
  shape.draw();
}

解决:这里不能自动导入这个类,只能手动导入不是很懂

import { Rectangle, Shape, Circle } from '../data/ShapeClass';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

let shapes: Shape[] = [];
shapes.push(new Rectangle());
shapes.push(new Circle());

for (let shape of shapes) {
  shape.draw();
}

手动导入之后就解决这个问题了

 

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐