Skip to content

Regression: Covariance fails in latest TS release? #29590

Closed
@nicojs

Description

@nicojs

TypeScript Version: 3.3.0-dev.20190125

Search Terms:
covariance

Code

type Injector<TContext> = {
  resolve: <T extends keyof TContext>() => T extends keyof TContext ? TContext[T] : never;
}

const foo: Injector<{ foo: string, bar: string }> = {} as any;
const bar: Injector<{ bar: string }> = foo; // no error
console.log(bar);

Expected behavior:
No compiler error with --strictFunctionTypes. As was the case in TS 3.2.2

Actual behavior:
Compiler error (since TS 3.2.4)

index.ts:18:7 - error TS2322: Type 'Injector<{ foo: string; bar: string; }>' is not assignable to type 'Injector<{ bar: string; }>'.
  Property 'foo' is missing in type '{ bar: string; }' but required in type '{ foo: string; bar: string; }'.

18 const bar: Injector<{ bar: string }> = foo;
         ~~~

  index.ts:17:23
    17 const foo: Injector<{ foo: string, bar: string }> = {} as any;
                             ~~~
    'foo' is declared here.

Playground Link:
https://www.typescriptlang.org/play/index.html#src=type%20Injector%3CTContext%3E%20%3D%20%7B%0D%0A%20%20resolve%3A%20%3CT%20extends%20keyof%20TContext%3E()%20%3D%3E%20T%20extends%20keyof%20TContext%20%3F%20TContext%5BT%5D%20%3A%20never%3B%0D%0A%7D%0D%0A%0D%0Aconst%20foo%3A%20Injector%3C%7B%20foo%3A%20string%2C%20bar%3A%20string%20%7D%3E%20%3D%20%7B%7D%20as%20any%3B%0D%0Aconst%20bar%3A%20Injector%3C%7B%20bar%3A%20string%20%7D%3E%20%3D%20foo%3B%20%2F%2F%20no%20error%0D%0Aconsole.log(bar)%3B

Related Issues:

(maybe): #28752

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