question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array of tuple literal not assignable to iterable of tuple with --lib es2015 -t es5

See original GitHub issue

TypeScript 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]>'.

Playground Link

Related Issues: maybe #29311 ?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dragomirtitiancommented, Aug 8, 2019

Cloned repo, ran tsc .\hello.ts -target es2015, no error.

Are you sure you don’t have another tsc installed on your system? Run a tsc- -v

0reactions
RyanCavanaughcommented, Aug 16, 2019

@rbuckton can you explain what’s happening? Bug?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found