Array of tuple literal not assignable to iterable of tuple with --lib es2015 -t es5
See original GitHub issueTypeScript Versions: 3.5.1, 3.5.3 (currently typescript@latest
), 3.6.0-beta (currently typescript@beta
), 3.6.0-dev.20190808 (currently typescript@next
)
Search Terms:
Code
// Works:
declare function foo<T>(i: Iterable<T>): T
const x = foo([1]); // infers a: number
// Fails:
declare function bar<T, U>(j: Iterable<[T, U]>): [T, U]
const y = bar([[1, 'a']]);
// ^^^^^^^^
// Argument of type '(string | number)[][]' is not assignable to
// parameter of type 'Iterable<[string | number, string | number]>'.
// workaround:
declare function qux<T, U>(j: Array<[T, U]> | Iterable<[T, U]>): [T, U]
const z = qux([[1, 'a']]); // infers z: [number, string]
Expected behavior: variable y
is inferred to be of type [number, string]
and parameter j
is inferred to be of type Array<[number, string]>
Actual behavior: error TS2345: Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[string | number, string | number]>'.
Related Issues: maybe #29311 ?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Unable to define type for tuple: Target requires 2 element(s ...
I'm not happy with the assertion required in as [platform, country] - but the as const approach will occasionally be problematic if the ......
Read more >Documentation - Iterators and Generators - TypeScript
An object is deemed iterable if it has an implementation for the Symbol.iterator property. Some built-in types like Array , Map , Set...
Read more >Overview - TypeScript
Building on that work, the new Generator type is an Iterator that always has both the return and throw methods present, and is...
Read more >Hacks for Creating JavaScript Arrays - freeCodeCamp
Creating Arrays: The Array Constructor. The most popular method for creating arrays is using the array literal syntax, which is very ...
Read more >TypeScript errors and how to fix them
error TS1192: Module ' json5 ' has no default export. Broken Code ❌. 1, import json5 from 'json5 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Cloned repo, ran
tsc .\hello.ts -target es2015
, no error.Are you sure you don’t have another
tsc
installed on your system? Run atsc- -v
@rbuckton can you explain what’s happening? Bug?