【问题标题】:Cannot pass Class to helper method无法将类传递给辅助方法
【发布时间】:2019-05-05 10:11:06
【问题描述】:

我有这个编译:

export namespace Foo {
   export namespace GET {
       export namespace Basic {
          export class Req {}
       }
   }
}

type Req = Foo.GET.Basic.Req;
e.setRequestBodyType(Foo.GET.Basic.Req);

我想做的是,将其减少为一行,如下所示:

type Req = typeof e.setRequestBodyType(Foo.GET.Basic.Req);

但是,我收到此错误:

tsc --watch 只是这样说:

express-test.ts:34:37 - 错误 TS1005: ';'预计。

34 type Req = typeof e.setRequestType(Foo.GET.Basic.Req);

有人知道如何完成我想做的事情吗?

现在辅助方法看起来像:

  setRequestType(v:any){
    return v;
  }

我试过这个:

  setRequestType<T>(v:T): T{
    return v;
  }      

然后我得到:

~

很奇怪。

【问题讨论】:

标签: tsc typescript3.0


【解决方案1】:

typeof 类型运算符只接受标识符,不接受方法调用。

请参阅spec

【讨论】:

    猜你喜欢
    • 2012-08-07
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多