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

io-ts recursion use issue #686

Open
t-pyrope opened this issue Jan 31, 2023 · 4 comments
Open

io-ts recursion use issue #686

t-pyrope opened this issue Jan 31, 2023 · 4 comments

Comments

@t-pyrope
Copy link

Hello! I'm trying to use t.recursion of the library to document a type, that has recursion. But I've got an error in IDE and can't understand it. Could I ask you for help?

The type in the documentation looks like this:

export interface BoxBase {
  id?: string;
  mode: DockMode;
  size?: number;
  children: (BoxBase | PanelBase)[];
}

I wrote this:

const PanelBaseV = t.intersection([
	t.type({
		tabs: t.array(TabBaseV),
	}),
	t.partial({
          // ...
	})
])

interface BoxBaseV {
	mode: t.TypeOf<typeof DockModeV>,
	children: Array<BoxBaseV | t.TypeOf<typeof PanelBaseV>>,
	id?: Id,
	size?: number,
}

const BoxBaseV: t.Type<BoxBaseV> = t.recursion('BoxBaseV', (self) =>
	t.intersection([
		t.type({
			mode: DockModeV,
			children: t.array(
				t.union([ self, PanelBaseV ])
			),
		}),
		t.partial({
			id: IdV,
			size: t.number,
		})
	])
)

Then I get an error in IDE:

Type 'LayoutBase' is not assignable to type '{ dockbox: BoxBaseV; } & { floatbox?: BoxBaseV | undefined; windowbox?: BoxBaseV | undefined; maxbox?: BoxBaseV | undefined; }'.
   Type 'LayoutBase' is not assignable to type '{ dockbox: BoxBaseV; }'.
     The types of 'dockbox.children' are incompatible between these types.
       Type '(BoxBase | PanelBase)[]' is not assignable to type '(BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; }))[]'.
         Type 'BoxBase | PanelBase' is not assignable to type 'BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; })'.
           Type 'BoxBase' is not assignable to type 'BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; })'.
             Type 'BoxBase' is not assignable to type 'BoxBaseV'.
               Types of property 'children' are incompatible.
                 Type '(BoxBase | PanelBase)[]' is not assignable to type '(BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; }))[]'.
                   Type 'BoxBase | PanelBase' is not assignable to type 'BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; })'.
                     Type 'BoxBase' is not assignable to type 'BoxBaseV | ({ tabs: { id: string; }[]; } & { id?: string | undefined; size?: number | undefined; oldSize?: number | undefined; activeId?: string | undefined; alwaysOnTop?: boolean | undefined; ... 4 more ...; h?: number | undefined; })'.
@photz
Copy link

photz commented Jan 31, 2023

What is LayoutBase?

@t-pyrope
Copy link
Author

t-pyrope commented Jan 31, 2023

@photz LayoutBase is:

interface LayoutBase {
  dockbox: BoxBase;
  floatbox?: BoxBase;
  windowbox?: BoxBase;
  maxbox?: BoxBase;
}

And LayoutBaseV (which is my re-definition using io-ts):

const LayoutBaseV = t.intersection([
	t.type({
		dockbox: BoxBaseV,
	}),
	t.partial({
		floatbox: BoxBaseV,
		windowbox: BoxBaseV,
		maxbox: BoxBaseV,
	}),
])

@photz
Copy link

photz commented Feb 3, 2023

Am I missing something or is LayoutBase not actually being used in the code you are showing us?
Could you create a minimal self-contained runnable example?
When posting an issue, the easier you make it for others to reproduce the problem, the more likely you are to be helped.

@t-pyrope
Copy link
Author

t-pyrope commented Feb 7, 2023

Hi, I just tried again in my IDE and there are no warnings anymore for some reason, can I close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants