Skip to content

Commit

Permalink
revert all the changes and use casting
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsonpl committed Aug 30, 2024
1 parent a500462 commit bf0da17
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,8 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) {
return circularRefs.has(`#/components/schemas/${schemaName}`);
}
);
// Helper to chunk an array of anyOf schemas into smaller arrays of a specified size in order to fix the typescript infer limitation
handlebarsInstance.registerHelper('createBatch', ({ anyOf }, size) => {
if (anyOf) {
const chunks = [];
for (let i = 0; i < anyOf.length; i += size) {
chunks.push(anyOf.slice(i, i + size));
}
return chunks;
}
});
handlebarsInstance.registerHelper('generateBatchName', function (title = '', index) {
return `${title}Batch${index + 1}`;
});

handlebarsInstance.registerHelper('checkIfShouldBatch', function ({ anyOf }, options) {
handlebarsInstance.registerHelper('shouldCastExplicitly', function ({ anyOf }, options) {
if (anyOf?.length > 2) {
// @ts-expect-error
return options.fn(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,17 @@ export type {{@key}} = {{> ts_type}};
export type {{@key}}Input = {{> ts_input_type }};
export const {{@key}}: z.ZodType<{{@key}}, ZodTypeDef, {{@key}}Input> = {{> zod_schema_item }};
{{else}}
{{#checkIfShouldBatch this}}
{{~#each (createBatch this 2)~}}
const {{generateBatchName ../title @index}} = z.{{#if ../discriminator.propertyName}}
discriminatedUnion('{{../discriminator.propertyName}}', [
{{else}}
union([
{{/if}}
{{#each this}}
{{~> zod_schema_item ~}},
{{/each}}
]);
{{~/each~}}
export type {{@key}} = z.infer<
{{~#each (createBatch this 2)~}}
| typeof {{generateBatchName ../title @index}}
{{~/each~}}
>;
export const {{@key}} = z.{{#if ../discriminator.propertyName}}
discriminatedUnion('{{../discriminator.propertyName}}', [
{{else}}
union([
{{/if}}
{{~#each (createBatch this 2)~}}
{{generateBatchName ../title @index}},
{{~/each~}}
]) as z.ZodType<{{@key}}>;
{{#shouldCastExplicitly this}}
// We need this type to infer from it below, but in the end we want to export a casted Type
// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
const {{@key}}Internal = {{> zod_schema_item}};

export type {{@key}} = z.infer<typeof {{@key}}Internal>;
export const {{@key}} = {{> zod_schema_item}} as z.ZodType<unknown> as z.ZodType<{{@key}}>;
{{else}}
export type {{@key}} = z.infer<typeof {{@key}}>;
export const {{@key}} = {{> zod_schema_item}};
{{/checkIfShouldBatch}}
{{/shouldCastExplicitly}}
{{/if}}
{{#if enum}}
{{#unless (isSingle enum)}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,26 @@ export const ExceptionListItemEntryMatchWildcard = z.object({
operator: ExceptionListItemEntryOperator,
});

const ExceptionListItemEntryBatch1 = z.discriminatedUnion('type', [
// We need this type to infer from it below, but in the end we want to export a casted Type
// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
const ExceptionListItemEntryInternal = z.discriminatedUnion('type', [
ExceptionListItemEntryMatch,
ExceptionListItemEntryMatchAny,
]);
const ExceptionListItemEntryBatch2 = z.discriminatedUnion('type', [
ExceptionListItemEntryList,
ExceptionListItemEntryExists,
]);
const ExceptionListItemEntryBatch3 = z.discriminatedUnion('type', [
ExceptionListItemEntryNested,
ExceptionListItemEntryMatchWildcard,
]);
export type ExceptionListItemEntry = z.infer<
| typeof ExceptionListItemEntryBatch1
| typeof ExceptionListItemEntryBatch2
| typeof ExceptionListItemEntryBatch3
>;
export const ExceptionListItemEntry = z.union([
ExceptionListItemEntryBatch1,
ExceptionListItemEntryBatch2,
ExceptionListItemEntryBatch3,
]) as z.ZodType<ExceptionListItemEntry>;

export type ExceptionListItemEntry = z.infer<typeof ExceptionListItemEntryInternal>;
export const ExceptionListItemEntry = z.discriminatedUnion('type', [
ExceptionListItemEntryMatch,
ExceptionListItemEntryMatchAny,
ExceptionListItemEntryList,
ExceptionListItemEntryExists,
ExceptionListItemEntryNested,
ExceptionListItemEntryMatchWildcard,
]) as z.ZodType<unknown> as z.ZodType<ExceptionListItemEntry>;

export type ExceptionListItemEntryArray = z.infer<typeof ExceptionListItemEntryArray>;
export const ExceptionListItemEntryArray = z.array(ExceptionListItemEntry);
7 changes: 6 additions & 1 deletion packages/kbn-ts-type-check-cli/run_type_check_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ run(

await procRunner.run('tsc', {
cmd: Path.relative(REPO_ROOT, require.resolve('typescript/bin/tsc')),
args: ['-b', relative, '--pretty', '--verbose'],
args: [
'-b',
relative,
'--pretty',
...(flagsReader.boolean('verbose') ? ['--verbose'] : []),
],
env: {
NODE_OPTIONS: '--max-old-space-size=8192',
},
Expand Down
Loading

0 comments on commit bf0da17

Please sign in to comment.