Mapped complex template keys=25

No-emit type-check timing for mapped complex template keys=25.

tsz is 6.4x faster 77 lines 3 KB

Timing

tsz
58.56ms
tsgo
374.54ms

Files

// Mapped Type Complex Template Expansion O(N²) stress test
// Targets: evaluate_rules/mapped.rs — N properties × expensive template evaluation
//
// Unlike simple homomorphic mapped types ({ [K in keyof T]: T[K] }) where the
// template is trivial, these use conditional types and nested mapped types in
// the template position, making each property evaluation expensive.

// Utility types with non-trivial evaluation
type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;
type Stringify<T> = { [K in keyof T]: T[K] extends number ? string : T[K] extends boolean ? 'true' | 'false' : T[K] extends string ? T[K] : string };
type Validate<T> = { [K in keyof T]: T[K] extends string ? { valid: true; value: T[K] } : T[K] extends number ? { valid: true; value: T[K] } : { valid: false; value: never } };
type Nullable<T> = { [K in keyof T]: T[K] | null | undefined };
type Promisify<T> = { [K in keyof T]: Promise<T[K]> };

// Complex conditional template: each property evaluation triggers conditional
// type distribution and nested type instantiation
type FormField<T> =
    T extends string ? { type: 'text'; value: T; validate: (v: string) => boolean }
  : T extends number ? { type: 'number'; value: T; validate: (v: number) => boolean }
  : T extends boolean ? { type: 'checkbox'; value: T; validate: (v: boolean) => boolean }
  : T extends (infer U)[] ? { type: 'list'; items: FormField<U>[]; validate: (v: U[]) => boolean }
  : T extends object ? { type: 'group'; fields: FormFields<T>; validate: (v: T) => boolean }
  : { type: 'unknown'; value: T };

type FormFields<T> = { [K in keyof T]: FormField<T[K]> };

interface BigModel {
    field0: string;
    field1: number;
    field2: boolean;
    field3: string[];
    field4: { nested: string; count: number };
    field5: string;
    field6: number;
    field7: boolean;
    field8: string[];
    field9: { nested: string; count: number };
    field10: string;
    field11: number;
    field12: boolean;
    field13: string[];
    field14: { nested: string; count: number };
    field15: string;
    field16: number;
    field17: boolean;
    field18: string[];
    field19: { nested: string; count: number };
    field20: string;
    field21: number;
    field22: boolean;
    field23: string[];
    field24: { nested: string; count: number };
}

// Each mapped type application evaluates a conditional template for 25 properties
type BigForm = FormFields<BigModel>;
type BigStringified = Stringify<BigModel>;
type BigValidated = Validate<BigModel>;
type BigNullable = Nullable<BigModel>;
type BigPromises = Promisify<BigModel>;
type BigDeepPartial = DeepPartial<BigModel>;

// Chained: each composition re-evaluates all 25 properties
type Chained1 = Nullable<Stringify<BigModel>>;
type Chained2 = Validate<Nullable<BigModel>>;
type Chained3 = FormFields<Nullable<BigModel>>;

declare const form: BigForm;
declare const stringified: BigStringified;
declare const validated: BigValidated;
declare const chained: Chained3;

const _f0 = form.field0;
const _s0 = stringified.field0;
const _v0 = validated.field0;
const _fLast = form.field24;
const _cLast = chained.field24;