Description
I noticed that you can assign a 5D array value to an array type that requires 6 or more dimensions. That's unfortunate, but the more concerning thing is how doing so can apparently break typechecks for nested arrays of any depth across the whole file.
A similar thing happens with deeply-nested object types.
The depth limiter for nested instantiations seems relevant, especially since the magic number here is 6 (or really "anything more than 5").
TypeScript Version: 4.5.2, 4.2.0-dev.20201221 and all other versions tested (3.3.3-4.1.2)
Search Terms: "multidimensional array" "deep array" "nested array" 6D "six-dimensional" "nested object" "deep object" "depth limiter" "deeply-nested" isDeeplyNestedType
Code
// All assignments in this file are invalid, but aren't rejected by the typechecker.
// Uncomment this and suddenly the rest of the file is typechecked correctly!
// const a2: string[][] = [[]]
// const a2ShouldFail: string[] = a2
// const a2ShouldAlsoFail: string[][][] = a2
// Comment this out and the rest of the file is typechecked correctly!
const a6: string[][][][][][] = [[[[[[]]]]]]
const a6ShouldFail: string[][][][][] = a6
const a6ShouldAlsoFail: string[][][][][][][] = a6
const b2: string[][] = [[]]
const b2ShouldFail: string[] = b2
const b2ShouldAlsoFail: string[][][] = b2
// 6+ depth seems to be the tipping point; see also: https://tsplay.dev/yNa9BN
Expected behavior:
Those mis-typed variable assignments should be flagged by the typechecker. More importantly, typechecking for each term should not be affected by the presence/absence/source order of unrelated code.
Actual behavior:
TypeScript doesn't complain about anything unless you uncomment the first block of code or comment out the second one.
Related Issues:
- Object literals with depth of ~10 (with arrays) stop reporting type inference #21771
- Not reporting error of deep object literal as expected #34619
- Exclude types originating in literals from recursion depth limiter check #34742
- Simplify constraint depth limiter logic #41972
- Fix deeply nested type check #14825
- TS 4.1 regression in type assertion of generic mapped type #41617
- Deep nested object type inference #34922