Skip to content

Strange behaviour with assignability and generics #36310

Closed
@ElusiveMori

Description

@ElusiveMori

TypeScript Version: 3.7.2

Search Terms:

I've read through the related issues and the origin behind this error message, but I still can't wrap my head around this particular case, and it feels like a bug to me. In particular, this feels extra weird because the issue goes away if I just add an extra generic parameter on the interface - even though they are really doing the same thing.

It feels like TypeScript should be able to figure out, that if I pass e.g. Hello1<(a: number) => void> to whoops(), then it should be able to resolve C into (a: number) => void and A into [number]. And if, for some reason, the callee specifies an incompatible set of generics to the functions, that is should fail then, instead of at the function definition.

There are some variations I've tried on the exact signature of the function, but I haven't found anything that works.

Expected behavior: Both examples compile.

Actual behavior: Only one example compiles. The other fails with:

Type 'C' does not satisfy the constraint '(...args: any[]) => void'.
  Type '(...args: A) => void' is not assignable to type '(...args: any[]) => void'.
    Types of parameters 'args' and 'args' are incompatible.
      Type 'any[]' is not assignable to type 'A'.
        'any[]' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype of constraint 'any[]'.

Related Issues: #29049

Code

// this doesn't work
interface Hello1<C extends (...args: any[]) => void> {
    _1: C
}

function whoops<A extends any[], C extends (...args: A) => void>(
    hello: Hello1<C>
) {}

// this does!
interface Hello2<A extends any[], C extends (...args: A) => void> {
    _1: A
    _2: C
}

function ok<A extends any[], C extends (...args: A) => void>(
    bye: Hello2<A, C>
) { }
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

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