[TypeScript]: Indexing type by two generics enums












0















I am trying to get the return type of a function from a type by an index, where the index is a generic type variable.



Code:



enum PROTOCOL { P1 = 0, P2 = 1 };
enum PROTOCOL_P1_SPACE { S11 = 0, S12 = 1 };
enum PROTOCOL_P2_SPACE { S21 = 0, S22 = 1 };

type OBJECT = {
[PROTOCOL.P1]: {
[PROTOCOL_P1_SPACE.S11]: { a: string },
[PROTOCOL_P1_SPACE.S12]: { b: Array<number> },
},
[PROTOCOL.P2]: {
[PROTOCOL_P2_SPACE.S21]: { c: number },
[PROTOCOL_P2_SPACE.S22]: { d: Buffer },
},
};

function getObject<
TProtocol extends PROTOCOL,
TSpace extends PROTOCOL_P1_SPACE | PROTOCOL_P2_SPACE
>(query: string): OBJECT[TProtocol][TSpace] { return call('getObject', query); };


TypeScript throws the error Type 'TSpace' cannot be used to index type 'OBJECT[TProtocol]'.
How to fix it?







P.S. VSCode can understand what i want:



const p1s1 = getObject<PROTOCOL.P1, PROTOCOL_P1_SPACE.S11>('p1s1');
const p2s1 = getObject<PROTOCOL.P2, PROTOCOL_P2_SPACE.S21>('p2s2');


It understand, that p1s1 is { a: string } and p2s1 is { c: number }










share|improve this question



























    0















    I am trying to get the return type of a function from a type by an index, where the index is a generic type variable.



    Code:



    enum PROTOCOL { P1 = 0, P2 = 1 };
    enum PROTOCOL_P1_SPACE { S11 = 0, S12 = 1 };
    enum PROTOCOL_P2_SPACE { S21 = 0, S22 = 1 };

    type OBJECT = {
    [PROTOCOL.P1]: {
    [PROTOCOL_P1_SPACE.S11]: { a: string },
    [PROTOCOL_P1_SPACE.S12]: { b: Array<number> },
    },
    [PROTOCOL.P2]: {
    [PROTOCOL_P2_SPACE.S21]: { c: number },
    [PROTOCOL_P2_SPACE.S22]: { d: Buffer },
    },
    };

    function getObject<
    TProtocol extends PROTOCOL,
    TSpace extends PROTOCOL_P1_SPACE | PROTOCOL_P2_SPACE
    >(query: string): OBJECT[TProtocol][TSpace] { return call('getObject', query); };


    TypeScript throws the error Type 'TSpace' cannot be used to index type 'OBJECT[TProtocol]'.
    How to fix it?







    P.S. VSCode can understand what i want:



    const p1s1 = getObject<PROTOCOL.P1, PROTOCOL_P1_SPACE.S11>('p1s1');
    const p2s1 = getObject<PROTOCOL.P2, PROTOCOL_P2_SPACE.S21>('p2s2');


    It understand, that p1s1 is { a: string } and p2s1 is { c: number }










    share|improve this question

























      0












      0








      0








      I am trying to get the return type of a function from a type by an index, where the index is a generic type variable.



      Code:



      enum PROTOCOL { P1 = 0, P2 = 1 };
      enum PROTOCOL_P1_SPACE { S11 = 0, S12 = 1 };
      enum PROTOCOL_P2_SPACE { S21 = 0, S22 = 1 };

      type OBJECT = {
      [PROTOCOL.P1]: {
      [PROTOCOL_P1_SPACE.S11]: { a: string },
      [PROTOCOL_P1_SPACE.S12]: { b: Array<number> },
      },
      [PROTOCOL.P2]: {
      [PROTOCOL_P2_SPACE.S21]: { c: number },
      [PROTOCOL_P2_SPACE.S22]: { d: Buffer },
      },
      };

      function getObject<
      TProtocol extends PROTOCOL,
      TSpace extends PROTOCOL_P1_SPACE | PROTOCOL_P2_SPACE
      >(query: string): OBJECT[TProtocol][TSpace] { return call('getObject', query); };


      TypeScript throws the error Type 'TSpace' cannot be used to index type 'OBJECT[TProtocol]'.
      How to fix it?







      P.S. VSCode can understand what i want:



      const p1s1 = getObject<PROTOCOL.P1, PROTOCOL_P1_SPACE.S11>('p1s1');
      const p2s1 = getObject<PROTOCOL.P2, PROTOCOL_P2_SPACE.S21>('p2s2');


      It understand, that p1s1 is { a: string } and p2s1 is { c: number }










      share|improve this question














      I am trying to get the return type of a function from a type by an index, where the index is a generic type variable.



      Code:



      enum PROTOCOL { P1 = 0, P2 = 1 };
      enum PROTOCOL_P1_SPACE { S11 = 0, S12 = 1 };
      enum PROTOCOL_P2_SPACE { S21 = 0, S22 = 1 };

      type OBJECT = {
      [PROTOCOL.P1]: {
      [PROTOCOL_P1_SPACE.S11]: { a: string },
      [PROTOCOL_P1_SPACE.S12]: { b: Array<number> },
      },
      [PROTOCOL.P2]: {
      [PROTOCOL_P2_SPACE.S21]: { c: number },
      [PROTOCOL_P2_SPACE.S22]: { d: Buffer },
      },
      };

      function getObject<
      TProtocol extends PROTOCOL,
      TSpace extends PROTOCOL_P1_SPACE | PROTOCOL_P2_SPACE
      >(query: string): OBJECT[TProtocol][TSpace] { return call('getObject', query); };


      TypeScript throws the error Type 'TSpace' cannot be used to index type 'OBJECT[TProtocol]'.
      How to fix it?







      P.S. VSCode can understand what i want:



      const p1s1 = getObject<PROTOCOL.P1, PROTOCOL_P1_SPACE.S11>('p1s1');
      const p2s1 = getObject<PROTOCOL.P2, PROTOCOL_P2_SPACE.S21>('p2s2');


      It understand, that p1s1 is { a: string } and p2s1 is { c: number }







      typescript typescript-typings






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 15:59









      Nick LatkovichNick Latkovich

      286




      286
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to make the type of TSpace be dependent on the type of TProtocol. One way to achieve this would be like this:



          function getObject<
          TProtocol extends PROTOCOL,
          TSpace extends keyof OBJECT[TProtocol]
          >(query: string): OBJECT[TProtocol][TSpace]





          share|improve this answer
























          • ty. It resolved my problem.

            – Nick Latkovich
            Jan 18 at 18:03











          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%2f54257455%2ftypescript-indexing-type-by-two-generics-enums%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














          You have to make the type of TSpace be dependent on the type of TProtocol. One way to achieve this would be like this:



          function getObject<
          TProtocol extends PROTOCOL,
          TSpace extends keyof OBJECT[TProtocol]
          >(query: string): OBJECT[TProtocol][TSpace]





          share|improve this answer
























          • ty. It resolved my problem.

            – Nick Latkovich
            Jan 18 at 18:03
















          1














          You have to make the type of TSpace be dependent on the type of TProtocol. One way to achieve this would be like this:



          function getObject<
          TProtocol extends PROTOCOL,
          TSpace extends keyof OBJECT[TProtocol]
          >(query: string): OBJECT[TProtocol][TSpace]





          share|improve this answer
























          • ty. It resolved my problem.

            – Nick Latkovich
            Jan 18 at 18:03














          1












          1








          1







          You have to make the type of TSpace be dependent on the type of TProtocol. One way to achieve this would be like this:



          function getObject<
          TProtocol extends PROTOCOL,
          TSpace extends keyof OBJECT[TProtocol]
          >(query: string): OBJECT[TProtocol][TSpace]





          share|improve this answer













          You have to make the type of TSpace be dependent on the type of TProtocol. One way to achieve this would be like this:



          function getObject<
          TProtocol extends PROTOCOL,
          TSpace extends keyof OBJECT[TProtocol]
          >(query: string): OBJECT[TProtocol][TSpace]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 18 at 16:10









          GrassatorGrassator

          80959




          80959













          • ty. It resolved my problem.

            – Nick Latkovich
            Jan 18 at 18:03



















          • ty. It resolved my problem.

            – Nick Latkovich
            Jan 18 at 18:03

















          ty. It resolved my problem.

          – Nick Latkovich
          Jan 18 at 18:03





          ty. It resolved my problem.

          – Nick Latkovich
          Jan 18 at 18:03


















          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%2f54257455%2ftypescript-indexing-type-by-two-generics-enums%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

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre