Skip to content

Type narrowing on Function.length for either callable type #18422

Closed
@mjbvz

Description

@mjbvz

TypeScript Version: 2.5.2

Code

interface IFoo {
  a: ((x: number) => string) | ((x: number, y: boolean) => string)
}

const z: IFoo = { 
  a(x: number) { return '' }
}

if (z.a.length === 1) {
  z.a(1)
}

Expected behavior:
Type of z.a is narrowed to (x: number) => string) from the if statement

Actual behavior:
Error:

'Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: number, y: boolean) => string)' has no compatible call signatures.'

I'm trying to express an interface that can implement a method in one of two ways. Let me know if there is a better way to express this

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions