Skip to content

No typescript error generated when using nested TypeBox types #636

@sanderalvin

Description

@sanderalvin

Hello

When using nested TypeBox types, then in some cases TypeScript errors are not generated.

Here is quite small code snippet where the issue is reproduced (on line 39 in problematicFunction body).

Using latest version "@sinclair/typebox": "0.31.17"
Using typescript version "typescript": "5.2.2"

import { Static, Type } from '@sinclair/typebox';

export type OA = Static<typeof OA>;
export const OA = Type.Object({
	uuid: Type.String(),
});

export type OAEnriched = Static<typeof OAEnriched>;
export const OAEnriched = Type.Object({
	uuid: Type.String(),
	uuid2: Type.Union([Type.Null(), Type.String()]),
});

export const OS = Type.Object({
	items: Type.Array(OA),
});

export type OSEnriched = Static<typeof OSEnriched>;
export const OSEnriched = Type.Object({
	items: Type.Array(OAEnriched),
});

export type OR = Static<typeof OR>;
export const OR = Type.Object({
	things: Type.Array(OS),
});

export type OREnriched = Static<typeof OREnriched>;
export const OREnriched = Type.Object({
	things: Type.Array(OSEnriched),
});

// when you comment out this function, then error from problematic function also disappears
function showProblem(or: OR): OREnriched {
	return or;
}

function problematicFunction(ors: OR[]): OREnriched[] {
	return ors; // this should trigger TS error, function signature expects OrderEnriched[] but Order[] is returned
}

When using native TS types instead of TypeBox, then the issue does not exist:

export type OA = {
	uuid: string;
};

export type OAEnriched = {
	uuid: string;
	uuid2: string | null;
};

export type OS = {
	items: OA[];
};

export type OSEnriched = {
	items: OAEnriched[];
};

export type OR = {
	things: OS[];
};

export type OREnriched = {
	things: OSEnriched[];
};

function showProblem(or: OR): OREnriched {
	return or;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function problematicFunction(ors: OR[]): OREnriched[] {
	return ors; // this  triggers TS error, which is good!
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions