Skip to content

strictFunctionTypes prevents an assignment not related to functions #28671

Open
@the-ress

Description

@the-ress

TypeScript Version: 3.3.0-dev.20181122

Search Terms:
covariance, contravariance, keyof, interface, class, generic

Code

interface IBase {
    foo: string;
}

interface IDerived extends IBase {
    bar: string;
}

type StringPropertyNames<T> = { [P in keyof T]: T[P] extends string ? P : never }[keyof T]
type StringProperties<T> = Pick<T, StringPropertyNames<T>>

interface Foo<T> {
    readonly Bar: StringProperties<T>;
}

let baseProperties: StringProperties<IBase>
let derivedProperties: StringProperties<IDerived>

let baseInterface: Foo<IBase>
let derivedInterface: Foo<IDerived>

baseProperties = derivedProperties // no error
baseInterface = derivedInterface

tsc test.ts --strictFunctionTypes

Expected behavior:
Compiles without errors.

Actual behavior:

test.ts:23:1 - error TS2322: Type 'Foo<IDerived>' is not assignable to type 'Foo<IBase>'.
  Property 'bar' is missing in type 'IBase' but required in type 'IDerived'.

23 baseInterface = derivedInterface
   ~~~~~~~~~~~~~

  test.ts:6:5
    6     bar: string;
          ~~~
    'bar' is declared here.

Playground Link:

Related Issues:
#24190

The documentation says:

Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly.

I couldn't find any information saying it should affect anything else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions