typescript:使用get / set表示公共函数

时间:2014-03-08 01:28:59

标签: javascript typescript

我看到get和set用于定义getter和setter。有没有办法可以将名称getset用于公共函数。例如,假设我有一个数据库操作,我喜欢命名方法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 {
//...
}
}

1 个答案:

答案 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 {};
    }
}

Try it

usage