Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mkantor opened this issue Dec 22, 2020 · 9 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@mkantor
Copy link
Contributor

mkantor commented Dec 22, 2020

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:

@mkantor
Copy link
Contributor Author

mkantor commented Dec 22, 2020

Please let me know if I should split this into two separate issues ("5D arrays can be assigned to 6+D array types" vs "6D array types can break uses of unrelated 2+D array types elsewhere in the file").

@mkantor mkantor changed the title Nested array types with 6+ dimensions can break array typechecking for the entire file Nested array/object types 6+ deep can break typechecking for smaller nested types across the entire file Dec 22, 2020
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 23, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 23, 2020
@zifahm
Copy link

zifahm commented Dec 15, 2021

any workaround?

@mkantor
Copy link
Contributor Author

mkantor commented Dec 15, 2021

@zifahm I believe this was fixed by #46599 which was included in TypeScript 4.5.3, so if you can upgrade your compiler that might help.

I haven't done extensive testing yet but at least the playground links above now exhibit correct behavior when using 4.5.3. Let me know if you upgrade and still see problems, if not I'll probably close this issue.

@mkantor
Copy link
Contributor Author

mkantor commented Dec 15, 2021

@zifahm your OrderFragment type specifies that OrderFragment["orderProduct"][0]["orderModifier"] is optional and possibly null.

Your playground has an error for the same reason that type X = (unknown[] | null | undefined)[0] is an error.

@zifahm
Copy link

zifahm commented Dec 15, 2021

@mkantor yeah just got it thanks :) 😅

@mkantor
Copy link
Contributor Author

mkantor commented Dec 16, 2021

Since I haven't been able to reproduce this issue since 4.5.3, I'm closing it. I'm very happy to see the depth limiter getting smarter! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants