Skip to content

Consider using union types for function return expressions #921

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

Via #919

Even with union types, we still require that a best common type can be selected from the types of the return expressions in a function:

function fn() { // Error, return expressions do not have a BCT
  if(x) {
    return 'foo';
  } else {
    return 42;
  }
}

If this is the desired behavior, the user must write

function fn(): number|string {

This is inconsistent with how we're treating other cases, e.g. var x = [1, 'foo']; produces anArray<number|string>.

Is the desired behavior here that we infer a more complex union type return type for a function, or that we enforce a consistent set of return expression types when no other information is given?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions