microsoft / microsoft/TypeScript

Strange behaviour with assignability and generics

Open
#36,311 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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: https://github.com/microsoft/TypeScript/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript 3.7.2 playground reproduction and compare the Hello1 and Hello2 generic declarations, then read related issue #29049 for context on the assignability error. Trace why the first example rejects C while the second compiles; done means establishing whether both examples should compile and covering the resolved behavior with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.