Microsoft/TypeScript

'super.prop' should only be allowed for accessors

Open

#42,214 opened on 2021年1月5日

GitHub で見る
 (1 comment) (10 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: classesHelp Wanted

説明

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.

コントリビューターガイド

'super.prop' should only be allowed for accessors · Microsoft/TypeScript#42214 | Good First Issue