Implementing an interface with a call signature and method returning “this”How to make a class implement a call signature in Typescript?TypeScript: Implement an interface with both call signature and indexing signatureTypeScript: Implement interface with both call signature and constructor signatureTypescript with jQuery UI Widgets - Implementing call signaturesHow to implement ng.IDirectiveFactory in TypeScriptAnonymous/inline interface implementation in TypeScriptTypescript not checking function argument types declared by interfacesUnion types and “currying” / partial application style APIsWriting TypeScript DefinitionTypescript: Interfaces vs TypesTypeScript compiler not enforcing type parameter in implementation of generic interface?Enumerate all classes implementing an interface in Typescripthow is implicit index signature type checked in typescript?

When did Bilbo and Frodo learn that Gandalf was a Maia?

"Mouth-breathing" as slang for stupidity

Why aren't rainbows blurred-out into nothing after they are produced?

Weird resistor with dots around it

Why does the cable resistance jump from a low value to high value at a particular frequency?

Pokemon Go: Gym Badge Over-completed?

What is the hottest thing in the universe?

Are employers legally allowed to pay employees in goods and services equal to or greater than the minimum wage?

Locked Room Murder!! How and who?

How did Arecibo detect methane lakes on Titan, and image Saturn's rings?

Attacking the Hydra

Help, I cannot decide when to start the story

Why aren’t there water shutoff valves for each room?

Good textbook for queueing theory and performance modeling

How can I find an old paper when the usual methods fail?

Co-workers with a lot of money and openly talk about it

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

Lípínguapua dopo Pêpê

Do beef farmed pastures net remove carbon emissions?

Is there a fallacy about "appeal to 'big words'"?

What is the prop for Thor's hammer made of?

What is the farthest a camera can see?

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

Link for download latest Edubuntu



Implementing an interface with a call signature and method returning “this”


How to make a class implement a call signature in Typescript?TypeScript: Implement an interface with both call signature and indexing signatureTypeScript: Implement interface with both call signature and constructor signatureTypescript with jQuery UI Widgets - Implementing call signaturesHow to implement ng.IDirectiveFactory in TypeScriptAnonymous/inline interface implementation in TypeScriptTypescript not checking function argument types declared by interfacesUnion types and “currying” / partial application style APIsWriting TypeScript DefinitionTypescript: Interfaces vs TypesTypeScript compiler not enforcing type parameter in implementation of generic interface?Enumerate all classes implementing an interface in Typescripthow is implicit index signature type checked in typescript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Preface: Our team is working on a library built on top of d3. Since we are using TypeScript, we are also using d3's types from DefinitelyTyped. The following question arises when trying to work with interfaces such as ScaleOrdinal and many others from there.




Suppose we have an interface containing both a call signature and additional properties:



export interface Foo<T> 
// Let's pretend this will be the identity function
(arg: T): T;

// Let's pretend that this will be a no-op function
// Note that this returns "this"
doFoo(): this;



How can we implement such an interface correctly & in a type-safe manner[1]? Upon research I've found the following related questions, all of which are slightly different and/or fairly old. I'd like to get an idea of whether we're missing something or whether to raise an issue with the TypeScript team here:



  1. How to make a class implement a call signature in Typescript?

  2. TypeScript: Implement interface with both call signature and constructor signature

  3. TypeScript: Implement an interface with both call signature and indexing signature

Note that the interface is external to us, thus implementing it is our only option.




¹ For the sake of the question, I would like the implementation to explicitly restate all type annotations.










share|improve this question






























    2















    Preface: Our team is working on a library built on top of d3. Since we are using TypeScript, we are also using d3's types from DefinitelyTyped. The following question arises when trying to work with interfaces such as ScaleOrdinal and many others from there.




    Suppose we have an interface containing both a call signature and additional properties:



    export interface Foo<T> 
    // Let's pretend this will be the identity function
    (arg: T): T;

    // Let's pretend that this will be a no-op function
    // Note that this returns "this"
    doFoo(): this;



    How can we implement such an interface correctly & in a type-safe manner[1]? Upon research I've found the following related questions, all of which are slightly different and/or fairly old. I'd like to get an idea of whether we're missing something or whether to raise an issue with the TypeScript team here:



    1. How to make a class implement a call signature in Typescript?

    2. TypeScript: Implement interface with both call signature and constructor signature

    3. TypeScript: Implement an interface with both call signature and indexing signature

    Note that the interface is external to us, thus implementing it is our only option.




    ¹ For the sake of the question, I would like the implementation to explicitly restate all type annotations.










    share|improve this question


























      2












      2








      2


      0






      Preface: Our team is working on a library built on top of d3. Since we are using TypeScript, we are also using d3's types from DefinitelyTyped. The following question arises when trying to work with interfaces such as ScaleOrdinal and many others from there.




      Suppose we have an interface containing both a call signature and additional properties:



      export interface Foo<T> 
      // Let's pretend this will be the identity function
      (arg: T): T;

      // Let's pretend that this will be a no-op function
      // Note that this returns "this"
      doFoo(): this;



      How can we implement such an interface correctly & in a type-safe manner[1]? Upon research I've found the following related questions, all of which are slightly different and/or fairly old. I'd like to get an idea of whether we're missing something or whether to raise an issue with the TypeScript team here:



      1. How to make a class implement a call signature in Typescript?

      2. TypeScript: Implement interface with both call signature and constructor signature

      3. TypeScript: Implement an interface with both call signature and indexing signature

      Note that the interface is external to us, thus implementing it is our only option.




      ¹ For the sake of the question, I would like the implementation to explicitly restate all type annotations.










      share|improve this question














      Preface: Our team is working on a library built on top of d3. Since we are using TypeScript, we are also using d3's types from DefinitelyTyped. The following question arises when trying to work with interfaces such as ScaleOrdinal and many others from there.




      Suppose we have an interface containing both a call signature and additional properties:



      export interface Foo<T> 
      // Let's pretend this will be the identity function
      (arg: T): T;

      // Let's pretend that this will be a no-op function
      // Note that this returns "this"
      doFoo(): this;



      How can we implement such an interface correctly & in a type-safe manner[1]? Upon research I've found the following related questions, all of which are slightly different and/or fairly old. I'd like to get an idea of whether we're missing something or whether to raise an issue with the TypeScript team here:



      1. How to make a class implement a call signature in Typescript?

      2. TypeScript: Implement interface with both call signature and constructor signature

      3. TypeScript: Implement an interface with both call signature and indexing signature

      Note that the interface is external to us, thus implementing it is our only option.




      ¹ For the sake of the question, I would like the implementation to explicitly restate all type annotations.







      typescript definitelytyped






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 11:13









      Ingo BürkIngo Bürk

      11.8k5 gold badges38 silver badges77 bronze badges




      11.8k5 gold badges38 silver badges77 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          In recent versions of typescript (3.2 or 3.3 not sure which) when you declare a function, you can also assign extra properties to the function and typescript will consider these as the definition for those properties and not complain about them not having been defined:



          export interface Foo<T> 
          (arg: T): T;
          doFoo(): this;


          function foo(arg: number) : number
          return arg

          foo.doFoo = function <TThis extends typeof foo>(this: TThis): TThis // no polymorphic this in simple functions
          return this


          let o: Foo<number> = foo; // foo is compatible with Foo<number>


          The old was of doing it, which still works is using Object.assign to create the function with extra properties:



          let o: Foo<number> = Object.assign(function (arg: number): number 
          return arg
          ,
          doFoo: function <TThis>(this: TThis): TThis
          return this

          )





          share|improve this answer



























          • Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

            – Ingo Bürk
            Mar 27 at 11:28






          • 1





            @IngoBürk also added a constraint on it to make foo members accessible

            – Titian Cernicova-Dragomir
            Mar 27 at 11:30










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375863%2fimplementing-an-interface-with-a-call-signature-and-method-returning-this%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In recent versions of typescript (3.2 or 3.3 not sure which) when you declare a function, you can also assign extra properties to the function and typescript will consider these as the definition for those properties and not complain about them not having been defined:



          export interface Foo<T> 
          (arg: T): T;
          doFoo(): this;


          function foo(arg: number) : number
          return arg

          foo.doFoo = function <TThis extends typeof foo>(this: TThis): TThis // no polymorphic this in simple functions
          return this


          let o: Foo<number> = foo; // foo is compatible with Foo<number>


          The old was of doing it, which still works is using Object.assign to create the function with extra properties:



          let o: Foo<number> = Object.assign(function (arg: number): number 
          return arg
          ,
          doFoo: function <TThis>(this: TThis): TThis
          return this

          )





          share|improve this answer



























          • Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

            – Ingo Bürk
            Mar 27 at 11:28






          • 1





            @IngoBürk also added a constraint on it to make foo members accessible

            – Titian Cernicova-Dragomir
            Mar 27 at 11:30















          1














          In recent versions of typescript (3.2 or 3.3 not sure which) when you declare a function, you can also assign extra properties to the function and typescript will consider these as the definition for those properties and not complain about them not having been defined:



          export interface Foo<T> 
          (arg: T): T;
          doFoo(): this;


          function foo(arg: number) : number
          return arg

          foo.doFoo = function <TThis extends typeof foo>(this: TThis): TThis // no polymorphic this in simple functions
          return this


          let o: Foo<number> = foo; // foo is compatible with Foo<number>


          The old was of doing it, which still works is using Object.assign to create the function with extra properties:



          let o: Foo<number> = Object.assign(function (arg: number): number 
          return arg
          ,
          doFoo: function <TThis>(this: TThis): TThis
          return this

          )





          share|improve this answer



























          • Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

            – Ingo Bürk
            Mar 27 at 11:28






          • 1





            @IngoBürk also added a constraint on it to make foo members accessible

            – Titian Cernicova-Dragomir
            Mar 27 at 11:30













          1












          1








          1







          In recent versions of typescript (3.2 or 3.3 not sure which) when you declare a function, you can also assign extra properties to the function and typescript will consider these as the definition for those properties and not complain about them not having been defined:



          export interface Foo<T> 
          (arg: T): T;
          doFoo(): this;


          function foo(arg: number) : number
          return arg

          foo.doFoo = function <TThis extends typeof foo>(this: TThis): TThis // no polymorphic this in simple functions
          return this


          let o: Foo<number> = foo; // foo is compatible with Foo<number>


          The old was of doing it, which still works is using Object.assign to create the function with extra properties:



          let o: Foo<number> = Object.assign(function (arg: number): number 
          return arg
          ,
          doFoo: function <TThis>(this: TThis): TThis
          return this

          )





          share|improve this answer















          In recent versions of typescript (3.2 or 3.3 not sure which) when you declare a function, you can also assign extra properties to the function and typescript will consider these as the definition for those properties and not complain about them not having been defined:



          export interface Foo<T> 
          (arg: T): T;
          doFoo(): this;


          function foo(arg: number) : number
          return arg

          foo.doFoo = function <TThis extends typeof foo>(this: TThis): TThis // no polymorphic this in simple functions
          return this


          let o: Foo<number> = foo; // foo is compatible with Foo<number>


          The old was of doing it, which still works is using Object.assign to create the function with extra properties:



          let o: Foo<number> = Object.assign(function (arg: number): number 
          return arg
          ,
          doFoo: function <TThis>(this: TThis): TThis
          return this

          )






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 11:29

























          answered Mar 27 at 11:23









          Titian Cernicova-DragomirTitian Cernicova-Dragomir

          92.1k5 gold badges74 silver badges92 bronze badges




          92.1k5 gold badges74 silver badges92 bronze badges















          • Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

            – Ingo Bürk
            Mar 27 at 11:28






          • 1





            @IngoBürk also added a constraint on it to make foo members accessible

            – Titian Cernicova-Dragomir
            Mar 27 at 11:30

















          • Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

            – Ingo Bürk
            Mar 27 at 11:28






          • 1





            @IngoBürk also added a constraint on it to make foo members accessible

            – Titian Cernicova-Dragomir
            Mar 27 at 11:30
















          Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

          – Ingo Bürk
          Mar 27 at 11:28





          Thanks! The trick of using this: TThis in the signature is what we seem to have been missing.

          – Ingo Bürk
          Mar 27 at 11:28




          1




          1





          @IngoBürk also added a constraint on it to make foo members accessible

          – Titian Cernicova-Dragomir
          Mar 27 at 11:30





          @IngoBürk also added a constraint on it to make foo members accessible

          – Titian Cernicova-Dragomir
          Mar 27 at 11:30








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375863%2fimplementing-an-interface-with-a-call-signature-and-method-returning-this%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript