UNPKG

ts-cast

Version:
42 lines (41 loc) 1.62 kB
type CommonPropertyAttributes = { configurable?: boolean; enumerable?: boolean; }; type ReadOnlyPropertyDescriptor<T> = { writable?: false; } & ({ value: T; } | { get(): T; }); type WritablePropertyDescriptor<T> = { value: T; writable: true; } | { get(): T; set(value: T): void; }; export type PropertyDescriptor<T> = CommonPropertyAttributes & (ReadOnlyPropertyDescriptor<T> | WritablePropertyDescriptor<T>); export type Property<N extends string | symbol | number, D extends PropertyDescriptor<any>> = D extends WritablePropertyDescriptor<infer T> ? { [p in N]: T; } : D extends ReadOnlyPropertyDescriptor<infer T> ? { readonly [p in N]: T; } : never; type ExistingFields<D extends {}> = Pick<D, { [p in keyof D]: D[p] extends never ? never : p; }[keyof D]>; export type Properties<D extends { [p in string | symbol | number]: PropertyDescriptor<any>; }> = ExistingFields<{ [p in keyof D]: D[p] extends WritablePropertyDescriptor<infer T> ? T : never; }> & ExistingFields<{ readonly [p in keyof D]: D[p] extends WritablePropertyDescriptor<any> ? never : D[p] extends ReadOnlyPropertyDescriptor<infer T> ? T : never; }>; export declare const defineProperty: <S, N extends string | number | symbol, D extends PropertyDescriptor<any>>(dest: S, name: N, descriptor: D) => Omit<S, N> & Property<N, D>; export declare const defineProperties: <S, D extends { [x: string]: PropertyDescriptor<any>; [x: number]: PropertyDescriptor<any>; [x: symbol]: PropertyDescriptor<any>; }>(dest: S, descriptors: D) => Omit<S, keyof D> & Properties<D>; export {};