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
When supplying an array to the initialValue argument of Array.reduce and specifying the type of that array as a multi type array (i.e Array.reduce<(string|number|boolean)[]>), it's possible to then supply an array typed to only one of those types to the initialValue argument (i.e string[]) without an error being raised.
🔎 Search Terms
typescript array reduce initial value bug
🕗 Version & Regression Information
This is the behaviour in every version I tried, and I reviewed the FAQ for entries about reduce
This is working as intended. Method parameters are intentionally bivariant, which leads to Array<T> being covariant in T; hence string[] is considered to be a subtype of (string | number | boolean)[]. This is unsound, but convenient. Strict soundness is not a goal of TypeScript (see Non-Goal #3). See this comment on #274 and this comment on #9825 for more information.
Bug Report
When supplying an array to the
initialValue
argument ofArray.reduce
and specifying the type of that array as a multi type array (i.eArray.reduce<(string|number|boolean)[]>
), it's possible to then supply an array typed to only one of those types to theinitialValue
argument (i.estring[]
) without an error being raised.🔎 Search Terms
typescript array reduce initial value bug
🕗 Version & Regression Information
reduce
⏯ Playground Link
Playground example
💻 Code
🙁 Actual behavior
Allows an incorrect array type to be supplied to
initialValue
argument🙂 Expected behavior
Supplying the incorrect array type to
InitialState
argument should raise a type errorThe text was updated successfully, but these errors were encountered: