Skip to content

Nested array/object types 6+ deep can break typechecking for smaller nested types across the entire file #42070

Closed
@mkantor

Description

@mkantor

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.

Playground Link:
https://www.typescriptlang.org/play?#code/PTAEEEBtNBDBneBLA5gOwLYFM0Bd6hJqi4AWSBAZkpFnAE51EBuskSAJgDSgBGArrgY4A5EMYArLAGNcWDnwCeJUnVyKADjNXSA1lnoA6AFDGQoAKpppAewzY8KinDQL4-DhxyRlZOo3ghG0oVOmpaQgJ1LWkdfQVbekZZHwBCMzBbNEC4ACYALlBA+iIUAG0AXUrQAF5QMsqKjNAsnNhcgGVSG35IDgAxWBpC4tLquvbm1qF2rp6+qHgbQeGi3BK0cqrt2rzTcwBhOwchMmcemddQ0ACgkL9QcKYozW0ZeJabJJlcNONpuAANhG6zG20qEJ2dQaMMaFXhTQBsEBc16AyGkBBGy2kMhu2R-xs2RmKO6aMWywxWLBuPBUKBpgBvAKa2xeOhjUJxL4nTJfRWmNZYN2zK5OWZqIWkCWAupm1pIty+zAgIA1KAvBoyEUsFgMFEbHw1KoSEgNBpSqANDYiLgANw6uhsJaFUi4XAaeD5ED4DSQWCKQxeZjARQAOVgAE4AEJhoA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions