You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
strictFunctionTypes prevents an assignment not related to functions #28671
interfaceIBase{foo: string;}interfaceIDerivedextendsIBase{bar: string;}typeStringPropertyNames<T>={[PinkeyofT]: T[P]extendsstring ? P : never}[keyofT]typeStringProperties<T>=Pick<T,StringPropertyNames<T>>interfaceFoo<T>{readonlyBar: StringProperties<T>;}letbaseProperties: StringProperties<IBase>letderivedProperties: StringProperties<IDerived>letbaseInterface: Foo<IBase>letderivedInterface: Foo<IDerived>baseProperties=derivedProperties// no errorbaseInterface=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.
Mmmm this might be one of those cases where the docs don't quite match reality. While strict function types is meant for toggling measuring variance for functions strictly, the flag actually enables variance probing on all types (when compared with themselves) as an optimization - so when comparing different instances of the same type reference or alias, we can see that StringPropertyNames is actually invariant on T, since our typical assignability rules fall flat when conditional types become involved (to be specific, we're way more conservative with conditional type assignability than is often useful).
Activity
j-oliveras commentedon Nov 26, 2018
Changing
interface Footo atypethe error disappear:weswigham commentedon Nov 26, 2018
Mmmm this might be one of those cases where the docs don't quite match reality. While strict function types is meant for toggling measuring variance for functions strictly, the flag actually enables variance probing on all types (when compared with themselves) as an optimization - so when comparing different instances of the same type reference or alias, we can see that
StringPropertyNamesis actually invariant onT, since our typical assignability rules fall flat when conditional types become involved (to be specific, we're way more conservative with conditional type assignability than is often useful).