0

Say you have a Typescript class like this:

 class CompExt extends Comp {
        public static sum(a: number, b: number): number {return a+b};
    };

The function sum will be sligtly different from the original class and it must be static.

Your actual code is

let ff: Function = CompExt;
console.log('the summ works fine',ff.sum(1,2));

Both the code editor and compilation will warn me this:

bas.ts(47,16): error TS2339: Property 'sum' does not exist on type 'Function'. 

Is there a way to avoid compilation errors? If I use any rather than Function everthing is fine but I was wondering if Typescript supports such style.

2 Answers 2

2

Just don't specify the type to be a Function because it's not a generic function, it's CompExt.

If you really want to specify the type, you can use: let ff: typeof CompExt = CompExt.

1
  • this is perfect. So I can have my editor autocompleting and correcting even if I use ff, which would not ocurr if I was using any or not typing. Thanks.
    – JLCDev
    Commented Sep 25, 2017 at 2:50
-1

A possible solution to your situation can be provided using decorators. Check this answer Static property on a class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.