TypeScript 用{}进行对象声明;

新手上路,请多包涵
class Point {
    x: number;
    y: number;
    private myAdd = function(x: string, y: string): void { console.log(x + y); }
    Invoke(x, y) { this.myAdd(x, y) }
}
let point:Point = {x:0,y:10, 
     myAdd (x: string, y: string): void { console.log(x + y); },
    Invoke(x, y) { this.myAdd(x, y) }};

报错信息:error TS2322: Type '{ x: number; y: number; myAdd(x: string, y: string): void; Invoke(x: any, y: any): void; }' is not assignable to type 'Point'.
Property 'myAdd' is private in type 'Point' but not in type '{ x: number; y: number; myAdd(x: string, y: string): void; Invoke(x: any, y: any): void; }'.

这种带有私有property/function的类只能用构造函数声明对象吗?

阅读 2.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进