Added type predicate to setDefault

This commit is contained in:
thepaperpilot 2021-08-17 18:39:11 -05:00
parent 8fff8f2835
commit 78ca3713a1

View file

@ -96,7 +96,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
RawGridFeatures<GridFeatures<Upgrade>, Upgrade>, RawGridFeatures<GridFeatures<Upgrade>, Upgrade>,
GridFeatures<Upgrade>, GridFeatures<Upgrade>,
Upgrade Upgrade
>(layer.id, layer.upgrades!); >(layer.id, layer.upgrades);
setRowCol(layer.upgrades); setRowCol(layer.upgrades);
for (const id in layer.upgrades.data) { for (const id in layer.upgrades.data) {
layer.upgrades.data[id].bought = function() { layer.upgrades.data[id].bought = function() {
@ -193,7 +193,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
RawGridFeatures<GridFeatures<Achievement>, Achievement>, RawGridFeatures<GridFeatures<Achievement>, Achievement>,
GridFeatures<Achievement>, GridFeatures<Achievement>,
Achievement Achievement
>(layer.id, layer.achievements!); >(layer.id, layer.achievements);
setRowCol(layer.achievements); setRowCol(layer.achievements);
for (const id in layer.achievements.data) { for (const id in layer.achievements.data) {
layer.achievements.data[id].earned = function() { layer.achievements.data[id].earned = function() {
@ -461,8 +461,8 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
); );
}; };
setDefault(player, "subtabs", {}); setDefault(player, "subtabs", {});
setDefault(player.subtabs!, layer.id, {}); setDefault(player.subtabs, layer.id, {});
setDefault(player.subtabs![layer.id], "mainTabs", Object.keys(layer.subtabs)[0]); setDefault(player.subtabs[layer.id], "mainTabs", Object.keys(layer.subtabs)[0]);
for (const id in layer.subtabs) { for (const id in layer.subtabs) {
layer.subtabs[id].active = function() { layer.subtabs[id].active = function() {
return playerProxy.subtabs[this.layer].mainTabs === this.id; return playerProxy.subtabs[this.layer].mainTabs === this.id;
@ -471,7 +471,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
if (layer.microtabs) { if (layer.microtabs) {
setDefault(player, "subtabs", {}); setDefault(player, "subtabs", {});
setDefault(player.subtabs!, layer.id, {}); setDefault(player.subtabs, layer.id, {});
for (const family in layer.microtabs) { for (const family in layer.microtabs) {
if (Object.keys(layer.microtabs[family]).length === 0) { if (Object.keys(layer.microtabs[family]).length === 0) {
console.warn( console.warn(
@ -497,7 +497,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
return firstUnlocked != undefined ? this[firstUnlocked] : undefined; return firstUnlocked != undefined ? this[firstUnlocked] : undefined;
}; };
setDefault( setDefault(
player.subtabs![layer.id], player.subtabs[layer.id],
family, family,
Object.keys(layer.microtabs[family]).find(tab => tab !== "activeMicrotab")! Object.keys(layer.microtabs[family]).find(tab => tab !== "activeMicrotab")!
); );
@ -594,7 +594,12 @@ function setupFeatures<T extends RawFeatures<R, S>, R extends Features<S>, S ext
} }
} }
function setDefault<T, K extends keyof T>(object: T, key: K, value: T[K], forceCached?: boolean) { function setDefault<T, K extends keyof T>(
object: T,
key: K,
value: T[K],
forceCached?: boolean
): asserts object is Exclude<T, K> & Required<Pick<T, K>> {
if (object[key] == undefined && value != undefined) { if (object[key] == undefined && value != undefined) {
object[key] = value; object[key] = value;
} }