我看到get和set用于定义getter和setter。有没有办法可以将名称get
或set
用于公共函数。例如,假设我有一个数据库操作,我喜欢命名方法get
,提供错误error TS1008: Unexpected token; '{' expected.
export interface IReq {
get: (r:string) => Object;
}
export class Req implements IReq{
constructor(){
this.get = (r) => this.getImpl(r);
}
get: (r:string) => Object; //gives error error TS1008: Unexpected token; '{' expected.
private getImpl(r:string) => Object {
//...
}
}
答案 0 :(得分:2)
get
是typescript中的上下文关键字,在上下文之外使用时不会妨碍。以下编译正常:
export interface IReq {
get: (r:string) => Object;
}
export class Req implements IReq{
constructor(){
this.get = (r) => this.getImpl(r);
}
get: (r:string) => Object; // No error
private getImpl(r:string): Object {
return {};
}
}