Skip to content

ts assumes a static function in an abstract class needs to be defined #43994

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
oakgary opened this issue May 7, 2021 · 3 comments
Closed

ts assumes a static function in an abstract class needs to be defined #43994

oakgary opened this issue May 7, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@oakgary
Copy link

oakgary commented May 7, 2021

Bug Report

🔎 Search Terms

This condition will always return true since the function is always defined. Did you mean to call it instead?ts(2774)
static method in abstract class

🕗 Version & Regression Information

This changed between versions 3.8.3 and 4.2.4.
The bug is not fixed in the nightly version.

⏯ Playground Link

Playground link with relevant code

💻 Code

abstract class A {
    static a: () => void;

    public static b(): void {
        if(this.a) {
            console.log('called function b and function a is defined');
        } else {
            console.log('called function b and function a is not defined')
        }
    }
}

class X extends A {

}

class Y extends A {
    static a() {
        //
    }
}

X.b();
Y.b();

🙁 Actual behavior

The error "This condition will always return true since the function is always defined. Did you mean to call it instead?ts(2774)" is displayed on this.a in function b of class A.

🙂 Expected behavior

A static function in an abstract class does not need to be defined. The error should therefore not be displayed.

@oakgary oakgary changed the title compiler assumes a static function in an abstract class needs to be defined ts assumes a static function in an abstract class needs to be defined May 7, 2021
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 7, 2021
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 7, 2021

Duplicate #27899; static members are assumed to have actually been initialized and what you're seeing here is a "missing" error. You should mark this property as optional if it's intentionally sometimes-not-present

@oakgary oakgary closed this as completed May 7, 2021
@Stirfry70
Copy link

@oakgary
Copy link
Author

oakgary commented May 7, 2021

Duplicate #27899; static members are assumed to have actually been initialized and what you're seeing here is a "missing" error. You should mark this property as optional if it's intentionally sometimes-not-present

Actually it is unintentional sometimes-not-present as you cannot make static methods abstract.
The check in function b is there to make sure it is defined.

For anyone stumbling upon this in the future. The error is resolved by marking the function as optional, as suggested by RyanCavanaugh:

abstract class A {
    static a?: () => void;

    public static b(): void {
        if(this.a) {
            console.log('called function b and function a is defined');
        } else {
            console.log('called function b and function a is not defined')
        }
    }
}

class X extends A {

}

class Y extends A {
    static a() {
        //
    }
}

X.b();
Y.b();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants