{"version":3,"file":"pick.mjs","sources":["../../src/record/pick.mts"],"sourcesContent":["import { expectType, Obj } from 'ts-data-forge';\nimport { type ArrayElement, type UnknownRecord } 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 picked. */\nexport const pick = <\n  const R extends UnknownRecord,\n  const KeysToPick extends readonly (keyof R & string)[],\n>(\n  recordType: Type<R>,\n  keysToPick: KeysToPick,\n  options?: Partial<\n    Readonly<{\n      typeName: string;\n      excessProperty: ExcessPropertyOption;\n    }>\n  >,\n): PickedType<R, KeysToPick> => {\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      `pick() 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.pick(shape, keysToPick), {\n    typeName:\n      options?.typeName ??\n      `Pick<${recordType.typeName}, ${toUnionKeyString(keysToPick)}>`,\n\n    excessProperty: options?.excessProperty ?? recordType.excessProperty,\n  }) as unknown as PickedType<R, KeysToPick>;\n};\n\ntype PickedType<\n  R extends UnknownRecord,\n  KeysToPick extends readonly (keyof R & string)[],\n> = Type<Readonly<Pick<R, ArrayElement<KeysToPick>>>>;\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  // pick narrows value type to picked keys\n  expectType<\n    TypeOf<ReturnType<typeof pick<TypeOf<Base>, readonly ['a', 'b']>>>,\n    Readonly<{ a: 0; b: 1 }>\n  >('=');\n\n  // pick with single key\n  expectType<\n    TypeOf<ReturnType<typeof pick<TypeOf<Base>, readonly ['c']>>>,\n    Readonly<{ c: 2 }>\n  >('=');\n}\n"],"names":[],"mappings":";;;;;;AAaO,MAAM,IAAA,GAAO,CAIlB,UAAA,EACA,UAAA,EACA,OAAA,KAM8B;AAC9B,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;;;;"}