Files
bitburner-src/src/types.ts
catloversg 8553bcb8fc MISC: Support compression of save data (#1162)
* Use Compression Streams API instead of jszip or other libraries.
* Remove usage of base64 in the new binary format.
* Do not convert binary data to string and back. The type of save data is SaveData, it's either string (old base64 format) or Uint8Array (new binary format).
* Proper support for interacting with electron-related code. Electron-related code assumes that save data is in the base64 format.
* Proper support for other tools (DevMenu, pretty-save.js). Full support for DevMenu will be added in a follow-up PR. Check the comments in src\DevMenu\ui\SaveFileDev.tsx for details.
2024-03-27 21:08:09 -07:00

50 lines
2.3 KiB
TypeScript

// The object properties on these numeric types are for typechecking only and do not exist at runtime. They're just a way to require a typechecking function.
export type Integer = number & { __Integer: true };
export type SafeInteger = Integer & { __isSafe: true };
export type PositiveNumber = number & { __Positive: true };
export type PositiveInteger = Integer & PositiveNumber;
export type PositiveSafeInteger = PositiveInteger & SafeInteger;
// Numeric typechecking functions - these should be moved somewhere else
export const isNumber = (n: unknown): n is number => !Number.isNaN(Number(n));
export const isInteger = (n: unknown): n is Integer => Number.isInteger(n);
export const isSafeInteger = (n: unknown): n is SafeInteger => Number.isSafeInteger(n);
export const isPositiveInteger = (n: unknown): n is PositiveInteger => isInteger(n) && n > 0;
export const isPositiveNumber = (n: unknown): n is PositiveNumber => isNumber(n) && n > 0;
export const isPositiveSafeInteger = (n: unknown): n is PositiveSafeInteger => isSafeInteger(n) && isPositiveInteger(n);
/** Utility type for typechecking objects. Makes all keys optional and sets values to unknown,
* making it safe to assert a shape for the variable once it's known to be a non-null object */
export type Unknownify<T> = {
[key in keyof T]?: unknown;
};
/** Get the member type of either an array or an object */
export type Member<T> = T extends (infer arrayMember)[] ? arrayMember : T[keyof T];
//** Get the keys of an object where the values match a given type */
export type TypedKeys<Obj, T> = { [K in keyof Obj]-?: Obj[K] extends T ? K : never }[keyof Obj];
/** Status object for functions that return a boolean indicating success/failure
* and an optional message */
export interface IReturnStatus {
res: boolean;
msg?: string;
}
/** Defines the minimum and maximum values for a range.
* It is up to the consumer if these values are inclusive or exclusive.
* It is up to the implementor to ensure max > min. */
export interface IMinMaxRange {
/** Value by which the bounds are to be divided for the final range */
divisor?: number;
/** The maximum bound of the range. */
max: number;
/** The minimum bound of the range. */
min: number;
}
// Type of save data. The base64 format is string, the binary format is Uint8Array.
export type SaveData = string | Uint8Array;