CODEBASE: Fix lint errors 3 (#1758)

This is a really big refactor because it actually *fixes* a lot of the lint errors instead of disabling them.
This commit is contained in:
catloversg
2024-11-14 23:18:57 +07:00
committed by GitHub
parent 97ca8c5f5e
commit 75cf9c88b5
31 changed files with 187 additions and 51 deletions
+14 -6
View File
@@ -2,13 +2,14 @@
import { ObjectValidator, validateObject } from "./Validator";
import { JSONMap, JSONSet } from "../Types/Jsonable";
import { loadActionIdentifier } from "../Bladeburner/utils/loadActionIdentifier";
import { objectAssert } from "./helpers/typeAssertion";
type JsonableClass = (new () => { toJSON: () => IReviverValue }) & {
fromJSON: (value: IReviverValue) => any;
fromJSON: (value: IReviverValue) => unknown;
validationData?: ObjectValidator<any>;
};
export interface IReviverValue<T = any> {
export interface IReviverValue<T = unknown> {
ctor: string;
data: T;
}
@@ -87,16 +88,23 @@ export function Generic_toJSON<T extends Record<string, any>>(
* @returns The object */
export function Generic_fromJSON<T extends Record<string, any>>(
ctor: new () => T,
// data can actually be anything. We're just pretending it has the right keys for T. Save data is not type validated.
data: Record<keyof T, any>,
data: unknown,
keys?: readonly (keyof T)[],
): T {
objectAssert(data);
const obj = new ctor();
// If keys were provided, just load the provided keys (if they are in the data)
if (keys) {
for (const key of keys) {
/**
* The type of key is "keyof T", but the type of data is Record<string, unknown>. TypeScript won't allow us to use
* key as the index of data, so we need to typecast here.
*/
for (const key of keys as string[]) {
const val = data[key];
if (val !== undefined) obj[key] = val;
if (val !== undefined) {
// @ts-expect-error -- TypeScript won't allow this action: Type 'T' is generic and can only be indexed for reading.
obj[key] = val;
}
}
return obj;
}
+34 -8
View File
@@ -1,5 +1,15 @@
// Various functions for asserting types.
export class TypeAssertionError extends Error {
friendlyType: string;
constructor(message: string, friendlyType: string, options?: ErrorOptions) {
super(message, options);
this.name = this.constructor.name;
this.friendlyType = friendlyType;
}
}
/** Function for providing custom error message to throw for a type assertion.
* @param v: Value to assert type of
* @param assertFn: Typechecking function to use for asserting type of v.
@@ -12,31 +22,47 @@ export function assert<T>(
try {
assertFn(v);
} catch (e) {
if (e instanceof TypeAssertionError) {
throw msgFn(e.friendlyType);
}
const type = typeof e === "string" ? e : "unknown";
throw msgFn(type);
}
}
/** Returns the friendlyType of v. arrays are "array" and null is "null". */
export function getFriendlyType(v: unknown): string {
function getFriendlyType(v: unknown): string {
return v === null ? "null" : Array.isArray(v) ? "array" : typeof v;
}
//All assertion functions used here should return the friendlyType of the input.
/** For non-objects, and for array/null, throws the friendlyType of v. */
export function objectAssert(v: unknown): asserts v is Partial<Record<string, unknown>> {
/** For non-objects, and for array/null, throws an error with the friendlyType of v. */
export function objectAssert(v: unknown): asserts v is Record<string, unknown> {
const type = getFriendlyType(v);
if (type !== "object") throw type;
if (type !== "object") {
console.error("The value is not an object. Value:", v);
throw new TypeAssertionError(
`The value is not an object. Its type is ${type}. Its string value is ${String(v)}.`,
type,
);
}
}
/** For non-string, throws the friendlyType of v. */
/** For non-string, throws an error with the friendlyType of v. */
export function stringAssert(v: unknown): asserts v is string {
const type = getFriendlyType(v);
if (type !== "string") throw type;
if (type !== "string") {
console.error("The value is not a string. Value:", v);
throw new TypeAssertionError(`The value is not an string. Its type is ${type}.`, type);
}
}
/** For non-array, throws the friendlyType of v. */
/** For non-array, throws an error with the friendlyType of v. */
export function arrayAssert(v: unknown): asserts v is unknown[] {
if (!Array.isArray(v)) throw getFriendlyType(v);
if (!Array.isArray(v)) {
console.error("The value is not an array. Value:", v);
const type = getFriendlyType(v);
throw new TypeAssertionError(`The value is not an array. Its type is ${type}.`, type);
}
}