Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardTypeScriptmethod bivariance strictFunctionTypesSingle-choice MCQ1 views

Under `--strictFunctionTypes`, why does the assignment `cmp = cmpDog` below compile even though `cmpDog`'s parameters are narrower than `cmp`'s, an assignment that would be rejected for an equivalent function-typed property?

interface Comparer<T> { compare(a: T, b: T): number } declare let cmp: Comparer<{ name: string }>; declare let cmpDog: Comparer<{ name: string; bark(): void }>; cmp = cmpDog;