Skip to content

Closure Array Compilation Error Code TS2322. #29058

Closed
@salmankhann

Description

@salmankhann

TypeScript Version: 3.2.2

Search Terms:
error TS2322: Types of property 'length' are incompatible. Type '2' is not assignable to type '1'.

Code


type ProxyComponent {
   simulate() {
       //Some Implementation
    }
}

type UserStory = {
    setup: () => [(component: ProxyComponent) => void]
};


//Compiles perfectly!
let userStory: UserStory = {
   setup: () => [(component) => component.simulate()]                   
}

//Fails to Compile!
/*
error TS2322: Type '[(component: ProxyComponent) => void, (component: ProxyComponent) => void]' is not assignable to type '[(component: ProxyComponent) => void]'.
      Types of property 'length' are incompatible.
        Type '2' is not assignable to type '1'.
*/
let userStory1: UserStory = {
   setup: () => [(component) => component.simulate(), (component) => component.simulate()]                   
}

I tried multiple things:

  1. Creating a Type for the closure and using it as the type of the setup property but that gives the same error. like type SetupStatement = () => [(component: ProxyComponent) => void]; but that didn't work either.
  2. Declaring the value of the setup method.

Expected behavior:
Code should compile correctly because the return types of the array match. It works perfectly for a single element but fails for multiple elements.

Actual behavior:
Compilation fails with the following error

error TS2322: Type '[(component: ProxyComponent) => void, (component: ProxyComponent) => void]' is not assignable to type '[(component: ProxyComponent) => void]'.
      Types of property 'length' are incompatible.
        Type '2' is not assignable to type '1'.

Playground Link:
Link to Playground

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions