-
Notifications
You must be signed in to change notification settings - Fork 199
Closed
Labels
Description
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!
}