mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-22 15:43:49 +01:00
REFACTOR: Better casting in JSONReviver.ts (#1780)
This commit is contained in:
parent
d824cd4fa6
commit
5faa418c74
@ -95,16 +95,12 @@ export function Generic_fromJSON<T extends Record<string, any>>(
|
|||||||
const obj = new ctor();
|
const obj = new ctor();
|
||||||
// If keys were provided, just load the provided keys (if they are in the data)
|
// If keys were provided, just load the provided keys (if they are in the data)
|
||||||
if (keys) {
|
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
|
// This cast is safe (T has string keys), but still needed because "keyof T" cannot be used to index data.
|
||||||
* key as the index of data, so we need to typecast here.
|
const val = data[key as string];
|
||||||
*/
|
|
||||||
for (const key of keys as string[]) {
|
|
||||||
const val = data[key];
|
|
||||||
if (val !== undefined) {
|
if (val !== undefined) {
|
||||||
// This is an unsafe assignment. We may load data with wrong types at runtime.
|
// This is an unsafe assignment. We may load data with wrong types at runtime.
|
||||||
// @ts-expect-error -- TypeScript won't allow this action: Type 'T' is generic and can only be indexed for reading.
|
obj[key] = val as T[keyof T];
|
||||||
obj[key] = val;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
|
Loading…
Reference in New Issue
Block a user