Skip to content

Bad type analysis with (very) complex generic types #27262

@sveyret

Description

@sveyret

TypeScript Version: 3.0.3

Code

I am using the intl-ts library. The following function does not compile because it complains that lang[result] is not known to be executable:

function convertResult<
  T extends Messages &
    { [P in K]: (fieldName: string, p1: P1, p2: P2, p3: P3, p4: P4) => string },
  K extends keyof T,
  P1 = any,
  P2 = any,
  P3 = any,
  P4 = any
>(
  result: K | null,
  params: [P1, P2, P3, P4],
  lang?: Intl<T>,
  fieldName?: string
): boolean | string | null {
  if (result === null) {
    return lang ? null : true
  } else {
    if (lang) {
      return lang[result](fieldName, ...params)
    } else {
      return false
    }
  }
}

Expected behavior:

Typescript should understand that lang[result] is a function.

Actual behavior:

Compiler gives error: “Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.”

Workaround:

The error can be removed by deleting the Messages inheritance in type T and changing lang from Intl<T> to T.

Playground Link:

A playground example with all needed types is provided here

Related Issues:

You also can refer to the question on StackOverflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions