Home > Enterprise >  What method(s) does the ts Function interface declare
What method(s) does the ts Function interface declare

Time:01-08

I recently started learning typescript & angular and then, I came across a short code like this interface transform extends Function{ (param:string):string; }

So I'm wondering, what kind of methods or return types the transform interface inherited from the Function interface? Again if I decide to have a method that returns transform like so: coordinate ():transform { }, what are the types expected of coordinate to return aside string?

CodePudding user response:

extends Function adds Function methods like call apply etc... and also the Function behavior, but it's redundant as you have a function definition inside the interface:

interface ExtendedFunction extends Function { }

declare const extendedFunction: ExtendedFunction;

extendedFunction.apply // Works
extendedFunction('bla', 1, true); // Works

interface Transform {
  (param: string): string;
}

declare const transform: Transform;

transform.apply // Works
transform('bla', 1, true) // Fails
transform('bla') // Works

TypeScript playground

  •  Tags:  
  • Related