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
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
add a comment |
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
add a comment |
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
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
typescript typescript-generics
edited Mar 7 at 18:00
reconbot
asked Mar 7 at 15:26
reconbotreconbot
2,65053657
2,65053657
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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!
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!
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
add a comment |
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!
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
add a comment |
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!
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!
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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