Skip to content

'call on undefined' error when calling super from overwritten property function #24678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gricey432 opened this issue Jun 5, 2018 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@gricey432
Copy link

TypeScript Version: 2.8.3, also whichever version powers playground

Search Terms:
Super function method property access cannot read property call of undefined

Code

class A {
    public myFunc = () => {
        return 'a';
    };
}

class B extends A {
    public myFunc = () => {
        return super.myFunc();
    };
}

const b = new B();
console.log(b.myFunc());

Expected behavior:
Either:

  • Prints 'a' to console (may not be possible based on related issues below?)
  • Compile time error

Actual behavior:
Successfully builds into JS which then fails at runtime

Uncaught TypeError: Cannot read property 'call' of undefined
    at B._this.myFunc (<anonymous>:24:44)
    at <anonymous>:31:15
    at HTMLButtonElement.excuteButton.onclick (https://www.typescriptlang.org/play/playground.js:247)

Playground Link: Link

Related Issues:
#4465 and #338 seem related though they focus on getters and setters

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jun 5, 2018

The runtime behavior itself seems appropriate. This is (more or less) the following equivalent code without class properties:

class A {
    constructor() {
        this.myFunc = () => {
            return 'a';
        };
    }
}

class B extends A {
    constructor() {
        super();
        this.myFunc = () => {
            return super.myFunc();
        };
    }
}

const b = new B();
console.log(b.myFunc());

I think what's problematic is that we don't differentiate between a property if it appeared on the prototype vs. on the instance, so we don't issue an error at all which is unfortunate.
and this will error in any runtime I've tried.

@ghost
Copy link

ghost commented Jun 5, 2018

I tested with typescript@2.8.3 and I still get a compile time error:

src/a.ts(9,22): error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 6, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@zhonghanwen
Copy link

Don't use =>, you can write:

class A {
    public myFunc ()  {
        return 'a';
    };
}

class B extends A {
    public myFunc ()  {
        return super.myFunc();
    };
}

const b = new B();
console.log(b.myFunc());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants