{"version":3,"file":"omit.mjs","sources":["../../src/record/omit.mts"],"sourcesContent":["import { expectType, Obj } from 'ts-data-forge';\nimport {\n  type ArrayElement,\n  type StrictOmit,\n  type UnknownRecord,\n} from 'ts-type-forge';\nimport {\n  type ExcessPropertyOption,\n  flattenShapeStructure,\n  hasRecordInternals,\n  type Type,\n  type TypeOf,\n} from '../type.mjs';\nimport { toUnionKeyString } from '../utils/index.mjs';\nimport { record } from './record.mjs';\n\n/** Creates a record type with keys omitted. */\nexport const omit = <\n  const R extends UnknownRecord,\n  const KeysToOmit extends readonly (keyof R & string)[],\n>(\n  recordType: Type<R>,\n  keysToOmit: KeysToOmit,\n  options?: Partial<\n    Readonly<{\n      typeName: string;\n      excessProperty: ExcessPropertyOption;\n    }>\n  >,\n): OmittedType<R, KeysToOmit> => {\n  if (!hasRecordInternals(recordType)) {\n    throw new Error(\n      `Expected a record type but received: ${recordType.typeName}`,\n    );\n  }\n\n  const shape = flattenShapeStructure(recordType.shapeStructure);\n\n  if (shape === undefined) {\n    throw new Error(\n      `omit() requires a simple or intersection record type, but received a union type`,\n    );\n  }\n\n  // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n  return record(Obj.omit(shape, keysToOmit), {\n    typeName:\n      options?.typeName ??\n      `Omit<${recordType.typeName}, ${toUnionKeyString(keysToOmit)}>`,\n\n    excessProperty: options?.excessProperty ?? recordType.excessProperty,\n  }) as unknown as OmittedType<R, KeysToOmit>;\n};\n\ntype OmittedType<\n  R extends UnknownRecord,\n  KeysToOmit extends readonly (keyof R & string)[],\n> = Type<Readonly<Omit<R, ArrayElement<KeysToOmit>>>>;\n\n// --- expectType assertions ---\n\n{\n  type Base = ReturnType<\n    typeof record<Readonly<{ a: Type<0>; b: Type<1>; c: Type<2> }>>\n  >;\n\n  // omit removes specified keys from value type\n  expectType<\n    TypeOf<ReturnType<typeof omit<TypeOf<Base>, readonly ['a']>>>,\n    Readonly<{ b: 1; c: 2 }>\n  >('=');\n\n  // omit with multiple keys\n  expectType<\n    TypeOf<ReturnType<typeof omit<TypeOf<Base>, readonly ['a', 'b']>>>,\n    Readonly<{ c: 2 }>\n  >('=');\n\n  expectType<\n    Omit<Readonly<{ a: Type<0>; b: Type<1>; c: Type<2> }>, 'a' | 'b'>,\n    Readonly<{\n      c: Type<2>;\n    }>\n  >('=');\n\n  expectType<\n    OmittedType<\n      Readonly<{ a: Type<0>; b: Type<1>; c: Type<2> }>,\n      // @ts-expect-error key \"d\" doesn't exist\n      readonly ['a', 'd']\n    >,\n    0\n  >('!=');\n}\n\n{\n  type A = Readonly<{ a: 0; b: 0; c: 0 } | { b: 0; c: 0; d: 0 }>;\n\n  // TypeScript Omit will not distribute over unions, so we don't support unions in omit() - it will produce an error if given a union type\n  expectType<Omit<A, 'b'>, Readonly<{ c: 0 }>>('=');\n\n  expectType<StrictOmit<A, 'b'>, Readonly<{ c: 0 }>>('=');\n\n  expectType<TypeOf<OmittedType<A, readonly ['b']>>, StrictOmit<A, 'b'>>('=');\n}\n"],"names":[],"mappings":";;;;;;AAiBO,MAAM,IAAA,GAAO,CAIlB,UAAA,EACA,UAAA,EACA,OAAA,KAM+B;AAC/B,EAAA,IAAI,CAAC,kBAAA,CAAmB,UAAU,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,qCAAA,EAAwC,WAAW,QAAQ,CAAA;AAAA,KAC7D;AAAA,EAAA;AAGF,EAAA,MAAM,KAAA,GAAQ,qBAAA,CAAsB,UAAA,CAAW,cAAc,CAAA;AAE7D,EAAA,IAAI,UAAU,MAAA,EAAW;AACvB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,+EAAA;AAAA,KACF;AAAA,EAAA;AAIF,EAAA,OAAO,MAAA,CAAO,GAAA,CAAI,IAAA,CAAK,KAAA,EAAO,UAAU,CAAA,EAAG;AAAA,IACzC,QAAA,EACE,SAAS,QAAA,IACT,CAAA,KAAA,EAAQ,WAAW,QAAQ,CAAA,EAAA,EAAK,gBAAA,CAAiB,UAAU,CAAC,CAAA,CAAA,CAAA;AAAA,IAE9D,cAAA,EAAgB,OAAA,EAAS,cAAA,IAAkB,UAAA,CAAW;AAAA,GACvD,CAAA;AACH;;;;"}