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
typeT=string|readonlystring[];// remove "readonly" to make this workletx: T='x'asT;if(Array.isArray(x)){throwError('unexpected array');}lety: string=x;// error here
Expected behavior:
Array.isArray should influence code-flow analysis even when array is marked readonly.
Actual behavior:
The type after the control-flow throws for array types still reports a union type that supports an array.
The negation of a type-guard works by filtering out all types in the source (here T) which can be assigned to the predicate type. Read-only arrays cannot be assigned to mutable arrays therefore readonly string[] is not filtered from T at x after the conditional.
There's no reason to think that m can't be a Dog ? It seems like the intuition about readonly array is just that's an Array with some special "readonly" bit set that doesn't actually change the type relationship, but that isn't the case.
TypeScript Version: 3.5.3
Search Terms: Array.isArray, readonly, control-flow analysis
Code
Expected behavior:
Array.isArray
should influence code-flow analysis even when array is markedreadonly
.Actual behavior:
The type after the control-flow throws for array types still reports a union type that supports an array.
Playground Link: https://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAKlC8UDOwBOBLAdgcygHylQgEMATAe0wBsRk0tsBtAXQG4AodqiYKADwBcsBFADkfUVGJJYHdugBmUABQBBVKmIgAdOiTrNIZXwCUJqAG92UKMAAWqcgHcoAUQ3lUy0QFdMEPkgAY2AIUikNLVETDgBfTm5eECEUDBwRPlYgA
Related Issues:
The text was updated successfully, but these errors were encountered: