Microsoft/TypeScript

'super.prop' should only be allowed for accessors

Open

#42,214 创建于 2021年1月5日

在 GitHub 查看
 (1 评论) (10 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: classesHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

super property access

🕗 Version & Regression Information

  • This changed in version v1.8.2

⏯ Playground Link

Playground link with relevant code

💻 Code

class Base {
    method() {}
    prop = 1;
    get accessor() { return 1 }
}

class Derived extends Base {
    fn() {
        super.method; // allowed, as expected
        super.accessor; // allowed for target >=ES2016, expected it to always be allowed
        super.prop; // allowed for target >=ES2016, expected it to always be disallowed, gives 'undefined' at runtime
    }
}

🙁 Actual behavior

For target <= ES5 both super.accessor and super.prop are an error, for target > ES5 both are allowed, although the runtime behavior is exactly the same. This change was introduced in #5860 because back then there was no (reliable) information whether a property was an accessor or a regular property. Now we have that information even in DTS files. The PR comments even mention that this change was not really correct.

Related: #24678 which was marked "Working as intended", although in the comments it was clearly stated that there should've been a compile error.

🙂 Expected behavior

Property access on super allows everything that resides on the prototype (methods and accessors) and disallows instance properties. Same logic should apply to all target ES versions as the behavior is always the same.

贡献者指南