Open
Description
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.
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.