Skip to content

Closure Array Compilation Error Code TS2322. #29058

Closed

Description

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:

Activity

  1. j-oliveras commented on Dec 17, 2018

    @j-oliveras
    Contributor

    In:

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

    you declares a setup method that return a tuple with only one element.

    If you want an array without limits declare it as:

    type UserStory = {
        setup: () => ((component: ProxyComponent) => void)[]
    };
  2. salmankhann commented on Dec 17, 2018

    @salmankhann
    Author

    Oops didn't know that that was the tuple syntax! There could be better error message from the compiler though.

    Thanks for helping!

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions