returning the union of the types from arrays2019 Community Moderator ElectionTypeScript: how to type guard union typeTypescript 1.4 Union types, false type mis-match errorCall new Date with union type “number | string”Typescript string literal with duck-typed objectTypeScript String Union to String ArrayTypeScript: Infer type of nested union typeInfer union types of type guards in TypeScriptInfer return type of sibling functionArgument of type 'NextHandleFunction' is not assignable to parameter of type 'PathParams'Conditional type with a Union

How to terminate ping <dest> &

I got the following comment from a reputed math journal. What does it mean?

PTIJ: Who should I vote for? (21st Knesset Edition)

Different outputs for `w`, `who`, `whoami` and `id`

How can we have a quark condensate without a quark potential?

Time travel from stationary position?

Does .bashrc contain syntax errors?

Relationship between sampajanna definitions in SN 47.2 and SN 47.35

Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)

Is it normal that my co-workers at a fitness company criticize my food choices?

Simplify an interface for flexibly applying rules to periods of time

Have the tides ever turned twice on any open problem?

Why did it take so long to abandon sail after steamships were demonstrated?

Did Ender ever learn that he killed Stilson and/or Bonzo?

Book: Young man exiled to a penal colony, helps to lead revolution

While on vacation my taxi took a longer route, possibly to scam me out of money. How can I deal with this?

Why do newer 737s use two different styles of split winglets?

Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?

What options are left, if Britain cannot decide?

A single argument pattern definition applies to multiple-argument patterns?

Examples of transfinite towers

English sentence unclear

Brexit - No Deal Rejection

World War I as a war of liberals against authoritarians?



returning the union of the types from arrays



2019 Community Moderator ElectionTypeScript: how to type guard union typeTypescript 1.4 Union types, false type mis-match errorCall new Date with union type “number | string”Typescript string literal with duck-typed objectTypeScript String Union to String ArrayTypeScript: Infer type of nested union typeInfer union types of type guards in TypeScriptInfer return type of sibling functionArgument of type 'NextHandleFunction' is not assignable to parameter of type 'PathParams'Conditional type with a Union










0















I'd like this merge method to have a return type of a union of all the different array types passed into it.



type AnyIterable<T> = Iterable<T> | AsyncIterable<T>

async function* merge<T>(...iterables: Array<AnyIterable<T>>)
for (const iterable of iterables)
yield* iterable



merge([1, 2, 3], ['1', '2', '3'])
// Argument of type 'string[]' is not assignable to parameter of type 'AnyIterable<number>'.
// Property '[Symbol.asyncIterator]' is missing in type 'string[]' but required in type 'AsyncIterable<number>'.ts(2345)


But I get that error. Obviously that's expected but I don't know how to type this to get the values from the array of iterables.










share|improve this question




























    0















    I'd like this merge method to have a return type of a union of all the different array types passed into it.



    type AnyIterable<T> = Iterable<T> | AsyncIterable<T>

    async function* merge<T>(...iterables: Array<AnyIterable<T>>)
    for (const iterable of iterables)
    yield* iterable



    merge([1, 2, 3], ['1', '2', '3'])
    // Argument of type 'string[]' is not assignable to parameter of type 'AnyIterable<number>'.
    // Property '[Symbol.asyncIterator]' is missing in type 'string[]' but required in type 'AsyncIterable<number>'.ts(2345)


    But I get that error. Obviously that's expected but I don't know how to type this to get the values from the array of iterables.










    share|improve this question


























      0












      0








      0








      I'd like this merge method to have a return type of a union of all the different array types passed into it.



      type AnyIterable<T> = Iterable<T> | AsyncIterable<T>

      async function* merge<T>(...iterables: Array<AnyIterable<T>>)
      for (const iterable of iterables)
      yield* iterable



      merge([1, 2, 3], ['1', '2', '3'])
      // Argument of type 'string[]' is not assignable to parameter of type 'AnyIterable<number>'.
      // Property '[Symbol.asyncIterator]' is missing in type 'string[]' but required in type 'AsyncIterable<number>'.ts(2345)


      But I get that error. Obviously that's expected but I don't know how to type this to get the values from the array of iterables.










      share|improve this question
















      I'd like this merge method to have a return type of a union of all the different array types passed into it.



      type AnyIterable<T> = Iterable<T> | AsyncIterable<T>

      async function* merge<T>(...iterables: Array<AnyIterable<T>>)
      for (const iterable of iterables)
      yield* iterable



      merge([1, 2, 3], ['1', '2', '3'])
      // Argument of type 'string[]' is not assignable to parameter of type 'AnyIterable<number>'.
      // Property '[Symbol.asyncIterator]' is missing in type 'string[]' but required in type 'AsyncIterable<number>'.ts(2345)


      But I get that error. Obviously that's expected but I don't know how to type this to get the values from the array of iterables.







      typescript typescript-generics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 18:00







      reconbot

















      asked Mar 7 at 15:26









      reconbotreconbot

      2,65053657




      2,65053657






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Hmm, I think people usually want errors like this to happen, since heterogeneous arrays are less common than homogeneous arrays, and type inference that always widened until it worked would not catch actual errors.



          If you want to work around this, it's possible the go the other direction: force the type inference to succeed, and then calculate the value of T from it if you need it:



          async function merge<I extends Array<AnyIterable<any>>>(...iterables: I) 
          for (const iterable of iterables)



          type UnArrayAnyIterable<A extends Array<AnyIterable<any>>> =
          A extends Array<AnyIterable<infer T>> ? T : never;


          Now this succeeds:



          merge([1, 2, 3], ['1', '2', '3']); // I inferred as the tuple `[number[], string[]]`


          And if you want T you can use UnArrayAnyIterable:



          declare function foo<I extends Array<AnyIterable<any>>>(...iterables: I): UnArrayAnyIterable<I>;
          const ret = foo([1, 2, 3], ['1', '2', '3']); // string | number


          Okay, hope that helps. Good luck!






          share|improve this answer























          • You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

            – reconbot
            Mar 7 at 17:59











          • It's a conditional type.

            – jcalz
            Mar 7 at 18:46










          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%2f55047319%2freturning-the-union-of-the-types-from-arrays%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














          Hmm, I think people usually want errors like this to happen, since heterogeneous arrays are less common than homogeneous arrays, and type inference that always widened until it worked would not catch actual errors.



          If you want to work around this, it's possible the go the other direction: force the type inference to succeed, and then calculate the value of T from it if you need it:



          async function merge<I extends Array<AnyIterable<any>>>(...iterables: I) 
          for (const iterable of iterables)



          type UnArrayAnyIterable<A extends Array<AnyIterable<any>>> =
          A extends Array<AnyIterable<infer T>> ? T : never;


          Now this succeeds:



          merge([1, 2, 3], ['1', '2', '3']); // I inferred as the tuple `[number[], string[]]`


          And if you want T you can use UnArrayAnyIterable:



          declare function foo<I extends Array<AnyIterable<any>>>(...iterables: I): UnArrayAnyIterable<I>;
          const ret = foo([1, 2, 3], ['1', '2', '3']); // string | number


          Okay, hope that helps. Good luck!






          share|improve this answer























          • You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

            – reconbot
            Mar 7 at 17:59











          • It's a conditional type.

            – jcalz
            Mar 7 at 18:46















          1














          Hmm, I think people usually want errors like this to happen, since heterogeneous arrays are less common than homogeneous arrays, and type inference that always widened until it worked would not catch actual errors.



          If you want to work around this, it's possible the go the other direction: force the type inference to succeed, and then calculate the value of T from it if you need it:



          async function merge<I extends Array<AnyIterable<any>>>(...iterables: I) 
          for (const iterable of iterables)



          type UnArrayAnyIterable<A extends Array<AnyIterable<any>>> =
          A extends Array<AnyIterable<infer T>> ? T : never;


          Now this succeeds:



          merge([1, 2, 3], ['1', '2', '3']); // I inferred as the tuple `[number[], string[]]`


          And if you want T you can use UnArrayAnyIterable:



          declare function foo<I extends Array<AnyIterable<any>>>(...iterables: I): UnArrayAnyIterable<I>;
          const ret = foo([1, 2, 3], ['1', '2', '3']); // string | number


          Okay, hope that helps. Good luck!






          share|improve this answer























          • You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

            – reconbot
            Mar 7 at 17:59











          • It's a conditional type.

            – jcalz
            Mar 7 at 18:46













          1












          1








          1







          Hmm, I think people usually want errors like this to happen, since heterogeneous arrays are less common than homogeneous arrays, and type inference that always widened until it worked would not catch actual errors.



          If you want to work around this, it's possible the go the other direction: force the type inference to succeed, and then calculate the value of T from it if you need it:



          async function merge<I extends Array<AnyIterable<any>>>(...iterables: I) 
          for (const iterable of iterables)



          type UnArrayAnyIterable<A extends Array<AnyIterable<any>>> =
          A extends Array<AnyIterable<infer T>> ? T : never;


          Now this succeeds:



          merge([1, 2, 3], ['1', '2', '3']); // I inferred as the tuple `[number[], string[]]`


          And if you want T you can use UnArrayAnyIterable:



          declare function foo<I extends Array<AnyIterable<any>>>(...iterables: I): UnArrayAnyIterable<I>;
          const ret = foo([1, 2, 3], ['1', '2', '3']); // string | number


          Okay, hope that helps. Good luck!






          share|improve this answer













          Hmm, I think people usually want errors like this to happen, since heterogeneous arrays are less common than homogeneous arrays, and type inference that always widened until it worked would not catch actual errors.



          If you want to work around this, it's possible the go the other direction: force the type inference to succeed, and then calculate the value of T from it if you need it:



          async function merge<I extends Array<AnyIterable<any>>>(...iterables: I) 
          for (const iterable of iterables)



          type UnArrayAnyIterable<A extends Array<AnyIterable<any>>> =
          A extends Array<AnyIterable<infer T>> ? T : never;


          Now this succeeds:



          merge([1, 2, 3], ['1', '2', '3']); // I inferred as the tuple `[number[], string[]]`


          And if you want T you can use UnArrayAnyIterable:



          declare function foo<I extends Array<AnyIterable<any>>>(...iterables: I): UnArrayAnyIterable<I>;
          const ret = foo([1, 2, 3], ['1', '2', '3']); // string | number


          Okay, hope that helps. Good luck!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 15:52









          jcalzjcalz

          28.2k22750




          28.2k22750












          • You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

            – reconbot
            Mar 7 at 17:59











          • It's a conditional type.

            – jcalz
            Mar 7 at 18:46

















          • You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

            – reconbot
            Mar 7 at 17:59











          • It's a conditional type.

            – jcalz
            Mar 7 at 18:46
















          You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

          – reconbot
          Mar 7 at 17:59





          You're fantastic at this. I appreciate all your help. Why does the UnArrayAnyIterable need to have a ? in it? I don't really understand the syntax.

          – reconbot
          Mar 7 at 17:59













          It's a conditional type.

          – jcalz
          Mar 7 at 18:46





          It's a conditional type.

          – jcalz
          Mar 7 at 18:46



















          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%2f55047319%2freturning-the-union-of-the-types-from-arrays%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

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived