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.

Wrong type inferred by returning a tuple

See original GitHub issue

TypeScript Version: 3.0.0-dev.20180707

Code

  const a: Array<[string, boolean]> = ["foo", "baz"].map(item => [item, !!item]);
  const b: Array<[string, boolean]> = ["foo", "baz"].map(item => { const value: [string, boolean] = [item, !!item]; return value;} );

Expected behavior: Both expressions should be allowed Actual behavior: assignment for b works, assignment for a doesn’t, as the compiler starts infering that the type for [item, !!item] is Array<string | boolean>.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jscheinycommented, Aug 2, 2018

Another example where a tuple inference would be desirable is in f0 below:

declare function f0<T extends any[]>(args: T): T;
declare function f1<T extends any[]>(...args: T): T;

f0(["a", 1]); // (string | number)[]
f1("a", 1); // [string, number]

This is currently blocking a typesafe implementation of Promise.all without overloads.

0reactions
austonpramodhcommented, Sep 25, 2019

+1 for different return types for different promises in promise.all

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript inferring Tuple as Array Incorrectly - Stack Overflow
1 Answer 1 · use a const assertion · give test an explicit tuple type.
Read more >
Tuples - Visual Basic | Microsoft Learn
Starting with Visual Basic 15.3, Visual Basic can infer the names of tuple elements; you do not have to assign them explicitly. Inferred...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
In the function greeting , the argument name is expected to be of type str and the return type str . Subtypes are...
Read more >
Wrong type in error message note when return type is tuple
When the tuples are of different lengths, the error message will use the types of the elements from the expected type in its...
Read more >
Type Errors - Pyre
Updating the return annotation, or the value returned from the function will resolve this error. 8: Incompatible Attribute Type​. Pyre will error if...
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