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
// Given something like this:declareconstmaybeArray: number|readonlynumber[];// Array.isArray is more difficult to use than it could be because it// is too eager to infer its argument (and therefore return) type to unknown[]// Type 'unknown' is not assignable to type 'number'.// @ts-expect-errorexportconstdefinitelyArray: readonlynumber[]=Array.isArray(maybeArray) ? maybeArray : [maybeArray];// Help inference with an explicit type paramexportconstdefinitelyArray2: readonlynumber[]=Array.isArray<typeofmaybeArray>(maybeArray) ? maybeArray : [maybeArray];
What is the feature you are proposing to solve the problem?
Redefine isArray more like this:
declareconstmaybeArray: number|readonlynumber[];declareconstmaybeArray2: unknown;// If isArray was instead defined simply like this:exportconstisArray=<T>(self: T): self is Extract<T,ReadonlyArray<unknown>>=>Array.isArray(self)// Then type inference would work without an explicit type argument// const definitelyArray: readonly number[]exportconstdefinitelyArray=isArray(maybeArray) ? maybeArray : [maybeArray];// And the unknown case would still work as expected// const definitelyArray2: unknown[]exportconstdefinitelyArray2=isArray(maybeArray2) ? maybeArray2 : [maybeArray2];
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered:
What is the problem this feature would solve?
What is the feature you are proposing to solve the problem?
Redefine
isArray
more like this:What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: