typescript - 'this' 类型仅在类或接口(interface)的非静态成员中可用

标签 typescript methods compiler-errors this

我试图定义一个类,其中一个构造函数参数属性声明并初始化类型为 (this: this) => string 的公共(public)成员,我得到一个 “this”类型仅在类或接口(interface)的非静态成员中可用错误。

这不会产生错误:

interface Foo {
    name: string; 
    isLocked: boolean;
    destination: string;
    description: (this: this) => string; 
}

const foo: Foo = {
    name: 'newdoor',
    isLocked: true,
    destination: 'someroom',
    description: function(this) {
        if(this.isLocked) return 'true'
        else return 'false'
    }
}

但是这样做:

class Bar {
    constructor(
        public name: string, 
        public isLocked: boolean,
        public destination: string,
        public description: (this: this) => string
    ) {}    
}

const bar = new Bar(
    'newdoor',
    true,
    'someroom',
    function(this) {
        if(this.isLocked) return 'true'
        else return 'false'
    }
)

有人可以解释为什么即使我确实在类的非静态成员中使用 this 类型也会出现此错误吗?此外,如果这是设计使然并且存在此错误的原因,那么为了能够创建一个类实例并为其提供一个可以访问该类实例的成员函数,有什么解决方法。

更新

如所提供的答案中所述,似乎不允许在构造函数参数属性中使用 this。但这留下了一个问题,那么我如何创建一个类实例,其中一个属性参数声明并初始化一个成员,该成员是一个需要访问该实例的其他成员的函数?

如果我这样做:

class Bar {
    constructor(
        public name: string, 
        public isLocked: boolean,
        public destination: string,
        public description: () => string
    ) {}    
}

const bar = new Bar(
    'newdoor',
    true,
    'someroom',
    function() {
        if(this.isLocked) return 'true'
        else return 'false'
    }
)

然后我得到一个 'this' implicitly has type 'any' because it does not have a type annotation 错误。这是有效的 JavaScript 代码,并且按照我的预期执行。对此进行编码的正确 TypeScript 方法是什么?

最佳答案

错误消息具有误导性,我不知道为什么第一个示例没有生成错误。但是第二个示例中的问题实际上是您试图将 this 用作描述值 this 的类型。在签名中,您已将 this 参数键入为它将成为的类型。在你的情况下,它必须是

(this: Bar) => string

此外,this 参数是假参数,它只告诉 TypeScript this 将在函数体中具有的类型。它不是您将作为参数传递给的参数。在 docs 中阅读相关信息.

您实际上可以在创建新实例时从传递的函数中省略 this 参数,因为 TypeScript 可以从函数签名中推断出 this 的类型类构造函数定义。

更新

As explained in the provided answers, it seems that the use of this is not allowed in a constructor parameter property.

显然我的回答不清楚,抱歉。这当然允许。但是你试图这样做:

(this: this) => string

这告诉 TypeScript 函数体中的 this(左侧)是类型 this(右侧)。但是 TypeScript 中没有名为 this 的类型,并且不允许声明一个,因为它是保留关键字。正如我上面所说,您必须将 this 声明为 Bar 类型。

这里是完整的例子:

class Bar {
    constructor(
        public name: string, 
        public isLocked: boolean,
        public destination: string,
        public description: (this: Bar) => string
    ) {}    
}

const bar = new Bar(
    'newdoor',
    true,
    'someroom',
    function() {
        if(this.isLocked) return 'true'
        else return 'false'
    }
)

关于typescript - 'this' 类型仅在类或接口(interface)的非静态成员中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334638/

相关文章:

c# - CS0029 读取用户输入时

javascript - Aurelia 对话框和 Typescript 异步等待 - 找不到名称 'await'

typescript - 导入 vue typescript 类型定义

typescript - 真正的setTimeout类型

node.js - `repository.create` 去除既是数组又是嵌入对象的列的值

java - 为什么我收到错误 "the Method Is Undefined for the type"?

java - 将数组传递给方法

c# - 电报消息的OpenTl自动更新引发错误CS0079 C#

delphi - 如何在任意类型上调用 GetEnumerator?

actionscript-3 - AS3尝试导入另一个.as文件时出现错误5001