1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| type InferValue<Prop extends PropertyKey, Desc> = Desc extends { get(): any; value: any; } ? never : Desc extends { value: infer T } ? Record<Prop, T> : Desc extends { get(): infer T } ? Record<Prop, T> : never;
type DefineProperty< Prop extends PropertyKey, Desc extends PropertyDescriptor > = Desc extends { writable: any; set(val: any): any; } ? never : Desc extends { writable: any; get(): any } ? never : Desc extends { writable: false } ? Readonly<InferValue<Prop, Desc>> : Desc extends { writable: true } ? InferValue<Prop, Desc> : Readonly<InferValue<Prop, Desc>>;
function defineProperty< Obj extends object, Key extends PropertyKey, PDesc extends PropertyDescriptor >( obj: Obj, prop: Key, val: PDesc ): asserts obj is Obj & DefineProperty<Key, PDesc> { Object.defineProperty(obj, prop, val); }
const storage = { currentValue: 0, };
defineProperty(storage, "maxValue", { value: 9001, writable: false, });
storage.maxValue = 100;
|