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

Deep nested object type inference #34922

Closed
eyal1990 opened this issue Nov 5, 2019 · 5 comments
Closed

Deep nested object type inference #34922

eyal1990 opened this issue Nov 5, 2019 · 5 comments
Labels
Fixed A PR has been merged for this issue

Comments

@eyal1990
Copy link

eyal1990 commented Nov 5, 2019

TypeScript Version: 3.6.4

Search Terms: Deep nested object type inference

Code

export interface A {
	b: B[];
}

export interface B {
	c: C[];
}

export interface C {
	d: D[];
}

export interface D {
	e: E[];
}
export interface E {
	id?: string;
}

// Doesn't work as expected
export const a1: A[] = [
	{
		b: [
			{
				c: [
					{
						d: [
							{
								e: [{ f: 'f' }], // Doesn't show error on f: 'f'
							},
						],
					},
				],
			},
		],
	},
];

// Work as expected
export const a2: A = {
	b: [
		{
			c: [
				{
					d: [
						{
							e: [{ f: 'f' }], // Does show error on f: 'f'
						},
					],
				},
			],
		},
	],
};

Expected behavior: Show error for unknown property 'f'

Actual behavior: Doesn't show error for unknown property 'f'

Playground Link:
http://www.typescriptlang.org/play/?ts=3.7-Beta#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgQTgG8AoASACMAuOAIQG0BdAbhIF8STRJZEU0suOsXLYaAYSasOXcNHjJUGHHnEiyAExoARKe07d5fJYLzb1wGgFE9HQ70UCVcK+oQaA-DQDOMKMgBzaQM5XmwIJF84TABGGnwmOABeOHpyUjJKGjTMsgzcsVTyXLzikq0iktz8qrJLVKI4dBoAcnQWuDZGABoykrZe2p6+sgG+4dyxzImxlhCeeHDI+EwAJnjk9WpK0oLsvprMipyqw9z6+kbmuDaOrsGqqZKJyYeyF6nhtlYgA
Related Issues:

@RyanCavanaugh RyanCavanaugh added the Fixed A PR has been merged for this issue label Nov 5, 2019
@RyanCavanaugh
Copy link
Member

This is fixed in the Nightly build

@fatcerberus
Copy link

Duplicate of #34619

Did that fix not make it into 3.7?

@RyanCavanaugh
Copy link
Member

The fix missed 3.7; it will be in 3.8

@dgreensp
Copy link

I'm still getting a version of this error in 3.8 nightly:

type Test = Test[] | { contents: Test }

function verify1(a: Test): Test {
  return a
}

function verify2<A extends Test>(a: A): A {
  return a
}

export const x1 = verify1([
  [
    {
      // correctly errors in 3.8, incorrectly passes type-checking in 3.7
      contents: [[[{ x: 1 }]]],
    },
  ],
])

export const x2 = verify2([
  [
    {
      // incorrectly passes type-checking in 3.7 and 3.8!
      contents: [[[{ x: 1 }]]],
    },
  ],
])

@dgreensp
Copy link

I'm a bit concerned about the "depth limiter" that only allows five recursive instantiations of a type; what does that mean for type design? I am designing some recursive types that keep their full structure in their type parameters, basically a DSL that is fully reflected in the type structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants