⬅️ Execute Program Advanced TypeScript
Function parameter type compatibility
- in typescript narrower types are assignable to wider types
- BUT! Functions that take narrower types are not assignable to functions that take wider types.
- For regular, non-function types, we can always assign narrower types to wider types. We can assign the literal type
'lastLoginDate'
to astring
variable because the former has a narrower type. - For function parameters, the same rule applies, but in reverse (we can assign functions that take wider parameters to function types that take narrower parameters.)
- It’s fine to pass a
'lastLoginDate'
to a function that expects astring
. It’s also fine to pass a[number, number]
to a function that expects anArray<number>
. - The technical term for what we’ve seen here is “contravariance”. In most strict static languages, function types are “contravariant on the parameter types”. That means “for function parameters, the normal rule about assigning narrower types to wider types is reversed”.