【问题标题】:Type signatures for this passed to free-standing functions将此类型签名传递给独立函数
【发布时间】:2014-05-25 17:34:26
【问题描述】:

代码如下:

interface Foo
{
    c : string
}

function foo()
{
    var c = this.c
    return c
}

foo.call({ c : "quux" })

Visual Studio 说 this : any 所以自动补全 this.c 不起作用。如何添加签名以告知 this 实现 Foo 以便 IDE 在 foo 内具有自动完成 this. 的类型信息?

【问题讨论】:

    标签: types typescript method-signature


    【解决方案1】:

    只需创建一个临时变量:

    var self:Foo = this;
    var c = self.c
    return c
    

    有一个悬而未决的问题是指定this 在任意上下文中的含义,您可以投票/参与:https://typescript.codeplex.com/workitem/507

    【讨论】:

      【解决方案2】:

      由于函数foo 可以与任何this 上下文一起使用,因此您需要使用类型断言:

      var c = (<Foo>this).c; 
      

      否则无法推断签名。

      【讨论】:

        猜你喜欢
        • 2019-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-23
        • 2015-04-04
        • 1970-01-01
        • 2017-04-15
        • 2011-02-20
        相关资源
        最近更新 更多