Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'runStaticMethod' is a static member of type 'DemoClass'

I am getting the following error when I build the following typescript code.

Property 'runStaticMethod' is a static member of type 'DemoClass'

Typescript Code:


export class Main {
    constructor(private demo: DemoClass) { }
    public run() {
        this.demo.runStaticMethod();
    }
}

export class DemoClass {
    public static runStaticMethod() {
        console.log('run...');
    }
}

new Main(DemoClass).run();

I am getting the following console error when I build the above typescript code. But the javascript code is running as expected.

Console Error:

Chitty:tsc NatarajanG$ tsc
src/index.ts:5:19 - error TS2576: Property 'runStaticMethod' is a static member of type 'DemoClass'

5         this.demo.runStaticMethod();
                    ~~~~~~~~~~~~~~~

Chitty:tsc NatarajanG$ 

like image 798
Natarajan Ganapathi Avatar asked Mar 20 '19 14:03

Natarajan Ganapathi


1 Answers

Because it's a static property and you should access it in the way how TS requires it: DemoClass.runStaticMethod(), despite javascript supports this.demo.runStaticMethod().

https://www.typescriptlang.org/docs/handbook/classes.html#static-properties

Each instance accesses this value through prepending the name of the class. Similarly to prepending this. in front of instance accesses, here we prepend Grid. in front of static accesses.

like image 145
satanTime Avatar answered Nov 06 '22 13:11

satanTime