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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.