Skip to content

Function return-type with decomposition not inferred correctly #2968

Closed

Description

@donnut
interface IAddressBook {}
interface INavModel {}

function foo() {
    let a: IAddressBook = {};
    let b: INavModel[] = [];
    return [a, b];
}

let [p, q] = foo();

let x: IAddressBook = p;
let y: INavModel[] = q;  // <- error

tsc (1.5.0-alpha) complains:

destruction.ts(13,5): error TS2322: Type 'IAddressBook' is not assignable to type 'INavModel[]'.
  Property 'length' is missing in type 'IAddressBook'.

atom-typescript shows that foo has signature foo(): IAddressBook[] and both p and q have type IAddressBook

The error is solved when specifying the return type of foo explicitly like:

function foo():  [IAddressBook, INavModel[]] {...}

Is this a known/explainable issue?

Activity

  1. changed the title [-]Decomposition seems to mix-up function return type[/-] [+]Function return-type with decomposition not inferred correctly[/+] on Apr 30, 2015
  2. RyanCavanaugh commented on Apr 30, 2015

    @RyanCavanaugh
    Member

    We don't infer tuple types from array literals.

    If you write code like this:

    var x = [cat, dog, animal];
    x[0] = new Dog(); // OK or not?

    We could either infer x's type as [Cat, Dog, Animal], or Animal[]. If we do the former, the next line of code is an error; if we do the latter, it's not.

    It's arguable in the abstract which behavior is preferable (you can make convincing examples either way just by changing the names of the variables), but we shipped the Animal[] behavior and didn't want to have a breaking change.

  3. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions