Infer return type of function based on type guard

Fuzzyma
:

I have the following definition:

function foo(arg: number[] | number) {
    if (Array.isArray(arg)) {
        return [1]
    }

    return 0
}

I would expect typescript to figure out automatically what the return type is. Because of the type guard isArray() it knows if arg is an Array and can show the return type as number[]. However, using foo(...) shows its return value as number[] | 0 even when passing an array.

  foo([]).push() // error because push doesnt exist on type 0

Is this a design limitation, a bug, just not implemented yet, or some other issue?

T.J. Crowder
:

I can't point to something definitively saying it's a design limitation, but I've seen experts like jcalz and Titian Cernicova-Dragomir citing various places where type inference is limited sometimes not because it couldn't do what we want, but because it would be too costly to do it (in terms of runtime cost or code complexity in the compiler). I suspect this fits into that category.

You probably know this, but for your specific example, you can use overloads to get the result you want:

function foo(arg: number[]): number[];
function foo(arg: number): number;
function foo(arg: number[] | number) {
    if (Array.isArray(arg)) {
        return arg.map(v => v * 2);
    }
    
    return arg * 2;
}
foo([]).push(10);

Playground link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Obtaining the return type of a function

From Java

Unable to infer complex closure return type with SwiftUI

From Java

TypeScript function return type based on input parameter

From Dev

How to infer correctly a return type for a template?

From Dev

infer lambda return type in template

From Dev

Can C++ templates infer return type?

From Dev

infer type parameter from function argument

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

infer template argument type from function return type

From Dev

Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

From Dev

Typescript: Constrain function generic type based on the expected return type

From Dev

How to infer the type of the function that can return type depending on condition?

From Dev

AngularJS: Return function based on input type

From Dev

Infer a return type of a method based on the type expected in call site?

From Dev

Why scala cannot infer common return type for a function with multiple parameter lists?

From Dev

Return delegate function based on type

From Dev

JSON infer class type based on Dictionary key

From Dev

Infer class generic property method return type

From Dev

TypeScript return exact function infer type

From Dev

Infer function generic type U from return value of passed function

From Dev

How to infer return type of Promise based on function arguments?

From Dev

Unable to infer complex closure return type; add explicit type to disambiguate?

From Dev

Typescript type guard for Type[keyof Type] function parameter

From Dev

Can typescript infer a function's response type based on parameter values?

From Dev

Scala method unable to infer return type

From Dev

infer template argument type from function return type

From Dev

JSON infer class type based on Dictionary key

From Dev

Why TypeScript can not infer type of recursive function

From Dev

Casting Function Return Type Based on Class Field

Related Related

  1. 1

    Obtaining the return type of a function

  2. 2

    Unable to infer complex closure return type with SwiftUI

  3. 3

    TypeScript function return type based on input parameter

  4. 4

    How to infer correctly a return type for a template?

  5. 5

    infer lambda return type in template

  6. 6

    Can C++ templates infer return type?

  7. 7

    infer type parameter from function argument

  8. 8

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  9. 9

    infer template argument type from function return type

  10. 10

    Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

  11. 11

    Typescript: Constrain function generic type based on the expected return type

  12. 12

    How to infer the type of the function that can return type depending on condition?

  13. 13

    AngularJS: Return function based on input type

  14. 14

    Infer a return type of a method based on the type expected in call site?

  15. 15

    Why scala cannot infer common return type for a function with multiple parameter lists?

  16. 16

    Return delegate function based on type

  17. 17

    JSON infer class type based on Dictionary key

  18. 18

    Infer class generic property method return type

  19. 19

    TypeScript return exact function infer type

  20. 20

    Infer function generic type U from return value of passed function

  21. 21

    How to infer return type of Promise based on function arguments?

  22. 22

    Unable to infer complex closure return type; add explicit type to disambiguate?

  23. 23

    Typescript type guard for Type[keyof Type] function parameter

  24. 24

    Can typescript infer a function's response type based on parameter values?

  25. 25

    Scala method unable to infer return type

  26. 26

    infer template argument type from function return type

  27. 27

    JSON infer class type based on Dictionary key

  28. 28

    Why TypeScript can not infer type of recursive function

  29. 29

    Casting Function Return Type Based on Class Field

HotTag

Archive