@aidc-toolkit/dev 0.9.19-beta → 0.9.21-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,3 +49,26 @@ export function omit<T extends object, K extends keyof T>(o: T, ...keys: K[]): O
49
49
  export function pick<T extends object, K extends keyof T>(o: T, ...keys: K[]): Pick<T, K> {
50
50
  return omitOrPick(false, o, ...keys);
51
51
  }
52
+
53
+ /**
54
+ * Cast a property as a more narrow type.
55
+ *
56
+ * @param o
57
+ * Object.
58
+ *
59
+ * @param key
60
+ * Key of property to cast.
61
+ *
62
+ * @returns
63
+ * Single-key object with property cast as desired type.
64
+ */
65
+ export function propertyAs<TAsType extends T[K], T extends object, K extends keyof T>(o: T, key: K): Readonly<Omit<T, K> extends T ? Partial<Record<K, TAsType>> : Record<K, TAsType>> {
66
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type is determined by condition.
67
+ return (key in o ?
68
+ {
69
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Force cast.
70
+ [key]: o[key] as TAsType
71
+ } :
72
+ {}
73
+ ) as ReturnType<typeof propertyAs<TAsType, T, K>>;
74
+ }