forked from profectus/Profectus
Made modifier typing a lot less nasty
This commit is contained in:
parent
4092cd6d56
commit
2e0e221010
3 changed files with 37 additions and 38 deletions
|
@ -4,7 +4,7 @@ import { jsx } from "features/feature";
|
||||||
import settings from "game/settings";
|
import settings from "game/settings";
|
||||||
import type { DecimalSource } from "util/bignum";
|
import type { DecimalSource } from "util/bignum";
|
||||||
import Decimal, { formatSmall } from "util/bignum";
|
import Decimal, { formatSmall } from "util/bignum";
|
||||||
import type { WithRequired } from "util/common";
|
import type { OmitOptional, OptionalKeys, RequiredKeys, WithRequired } from "util/common";
|
||||||
import type { Computable, ProcessedComputable } from "util/computed";
|
import type { Computable, ProcessedComputable } from "util/computed";
|
||||||
import { convertComputable } from "util/computed";
|
import { convertComputable } from "util/computed";
|
||||||
import { createLazyProxy } from "util/proxies";
|
import { createLazyProxy } from "util/proxies";
|
||||||
|
@ -38,16 +38,11 @@ export interface Modifier {
|
||||||
description?: ProcessedComputable<CoercableComponent>;
|
description?: ProcessedComputable<CoercableComponent>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Utility type that represents the output of all modifiers that represent a single operation. */
|
||||||
* Utility type used to narrow down a modifier type that will have a description and/or enabled property based on optional parameters, T and S (respectively).
|
export type OperationModifier<T> = WithRequired<
|
||||||
*/
|
Modifier,
|
||||||
export type ModifierFromOptionalParams<T, S> = undefined extends T
|
"invert" | "getFormula" | Extract<RequiredKeys<T>, keyof Modifier>
|
||||||
? undefined extends S
|
>;
|
||||||
? Omit<WithRequired<Modifier, "invert" | "getFormula">, "description" | "enabled">
|
|
||||||
: Omit<WithRequired<Modifier, "invert" | "enabled" | "getFormula">, "description">
|
|
||||||
: undefined extends S
|
|
||||||
? Omit<WithRequired<Modifier, "invert" | "description" | "getFormula">, "enabled">
|
|
||||||
: WithRequired<Modifier, "invert" | "enabled" | "description" | "getFormula">;
|
|
||||||
|
|
||||||
/** An object that configures an additive modifier via {@link createAdditiveModifier}. */
|
/** An object that configures an additive modifier via {@link createAdditiveModifier}. */
|
||||||
export interface AdditiveModifierOptions {
|
export interface AdditiveModifierOptions {
|
||||||
|
@ -65,9 +60,9 @@ export interface AdditiveModifierOptions {
|
||||||
* Create a modifier that adds some value to the input value.
|
* Create a modifier that adds some value to the input value.
|
||||||
* @param optionsFunc Additive modifier options.
|
* @param optionsFunc Additive modifier options.
|
||||||
*/
|
*/
|
||||||
export function createAdditiveModifier<T extends AdditiveModifierOptions>(
|
export function createAdditiveModifier<T extends AdditiveModifierOptions, S = OperationModifier<T>>(
|
||||||
optionsFunc: OptionsFunc<T>
|
optionsFunc: OptionsFunc<T>
|
||||||
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
|
) {
|
||||||
return createLazyProxy(feature => {
|
return createLazyProxy(feature => {
|
||||||
const { addend, description, enabled, smallerIsBetter } = optionsFunc.call(
|
const { addend, description, enabled, smallerIsBetter } = optionsFunc.call(
|
||||||
feature,
|
feature,
|
||||||
|
@ -111,7 +106,7 @@ export function createAdditiveModifier<T extends AdditiveModifierOptions>(
|
||||||
</div>
|
</div>
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
|
}) as S;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An object that configures an multiplicative modifier via {@link createMultiplicativeModifier}. */
|
/** An object that configures an multiplicative modifier via {@link createMultiplicativeModifier}. */
|
||||||
|
@ -130,9 +125,10 @@ export interface MultiplicativeModifierOptions {
|
||||||
* Create a modifier that multiplies the input value by some value.
|
* Create a modifier that multiplies the input value by some value.
|
||||||
* @param optionsFunc Multiplicative modifier options.
|
* @param optionsFunc Multiplicative modifier options.
|
||||||
*/
|
*/
|
||||||
export function createMultiplicativeModifier<T extends MultiplicativeModifierOptions>(
|
export function createMultiplicativeModifier<
|
||||||
optionsFunc: OptionsFunc<T>
|
T extends MultiplicativeModifierOptions,
|
||||||
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
|
S = OperationModifier<T>
|
||||||
|
>(optionsFunc: OptionsFunc<T>) {
|
||||||
return createLazyProxy(feature => {
|
return createLazyProxy(feature => {
|
||||||
const { multiplier, description, enabled, smallerIsBetter } = optionsFunc.call(
|
const { multiplier, description, enabled, smallerIsBetter } = optionsFunc.call(
|
||||||
feature,
|
feature,
|
||||||
|
@ -175,7 +171,7 @@ export function createMultiplicativeModifier<T extends MultiplicativeModifierOpt
|
||||||
</div>
|
</div>
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
|
}) as S;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An object that configures an exponential modifier via {@link createExponentialModifier}. */
|
/** An object that configures an exponential modifier via {@link createExponentialModifier}. */
|
||||||
|
@ -196,9 +192,10 @@ export interface ExponentialModifierOptions {
|
||||||
* Create a modifier that raises the input value to the power of some value.
|
* Create a modifier that raises the input value to the power of some value.
|
||||||
* @param optionsFunc Exponential modifier options.
|
* @param optionsFunc Exponential modifier options.
|
||||||
*/
|
*/
|
||||||
export function createExponentialModifier<T extends ExponentialModifierOptions>(
|
export function createExponentialModifier<
|
||||||
optionsFunc: OptionsFunc<T>
|
T extends ExponentialModifierOptions,
|
||||||
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
|
S = OperationModifier<T>
|
||||||
|
>(optionsFunc: OptionsFunc<T>) {
|
||||||
return createLazyProxy(feature => {
|
return createLazyProxy(feature => {
|
||||||
const { exponent, description, enabled, supportLowNumbers, smallerIsBetter } =
|
const { exponent, description, enabled, supportLowNumbers, smallerIsBetter } =
|
||||||
optionsFunc.call(feature, feature);
|
optionsFunc.call(feature, feature);
|
||||||
|
@ -263,7 +260,7 @@ export function createExponentialModifier<T extends ExponentialModifierOptions>(
|
||||||
</div>
|
</div>
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
|
}) as S;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -274,15 +271,9 @@ export function createExponentialModifier<T extends ExponentialModifierOptions>(
|
||||||
* @see {@link createModifierSection}.
|
* @see {@link createModifierSection}.
|
||||||
*/
|
*/
|
||||||
export function createSequentialModifier<
|
export function createSequentialModifier<
|
||||||
T extends Modifier[],
|
T extends Modifier,
|
||||||
S = T extends WithRequired<Modifier, "invert">[]
|
S = WithRequired<Modifier, Extract<RequiredKeys<T>, keyof Modifier>>
|
||||||
? T extends WithRequired<Modifier, "getFormula">[]
|
>(modifiersFunc: () => T[]) {
|
||||||
? WithRequired<Modifier, "description" | "invert" | "getFormula">
|
|
||||||
: Omit<WithRequired<Modifier, "description" | "getFormula">, "invert">
|
|
||||||
: T extends WithRequired<Modifier, "invert">[]
|
|
||||||
? WithRequired<Modifier, "getFormula" | "invert">
|
|
||||||
: Omit<WithRequired<Modifier, "getFormula">, "invert">
|
|
||||||
>(modifiersFunc: () => T): S {
|
|
||||||
return createLazyProxy(() => {
|
return createLazyProxy(() => {
|
||||||
const modifiers = modifiersFunc();
|
const modifiers = modifiersFunc();
|
||||||
|
|
||||||
|
@ -325,7 +316,7 @@ export function createSequentialModifier<
|
||||||
))
|
))
|
||||||
: undefined
|
: undefined
|
||||||
};
|
};
|
||||||
}) as unknown as S;
|
}) as S;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An object that configures a modifier section via {@link createModifierSection}. */
|
/** An object that configures a modifier section via {@link createModifierSection}. */
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
|
export type RequiredKeys<T> = {
|
||||||
|
[K in keyof T]-?: NonNullable<unknown> extends Pick<T, K> ? never : K;
|
||||||
|
}[keyof T];
|
||||||
|
export type OptionalKeys<T> = {
|
||||||
|
[K in keyof T]-?: NonNullable<unknown> extends Pick<T, K> ? K : never;
|
||||||
|
}[keyof T];
|
||||||
|
|
||||||
|
export type OmitOptional<T> = Pick<T, RequiredKeys<T>>;
|
||||||
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
|
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
|
||||||
|
|
||||||
export type ArrayElements<T extends ReadonlyArray<unknown>> = T extends ReadonlyArray<infer S>
|
export type ArrayElements<T extends ReadonlyArray<unknown>> = T extends ReadonlyArray<infer S>
|
||||||
|
|
|
@ -133,14 +133,14 @@ describe("Exponential Modifiers", () =>
|
||||||
testModifiers(createExponentialModifier, "exponent", Decimal.pow));
|
testModifiers(createExponentialModifier, "exponent", Decimal.pow));
|
||||||
|
|
||||||
describe("Sequential Modifiers", () => {
|
describe("Sequential Modifiers", () => {
|
||||||
function createModifier(
|
function createModifier<T extends Partial<ModifierConstructorOptions>>(
|
||||||
value: Computable<DecimalSource>,
|
value: Computable<DecimalSource>,
|
||||||
options: Partial<ModifierConstructorOptions> = {}
|
options?: T
|
||||||
): WithRequired<Modifier, "invert" | "getFormula"> {
|
) {
|
||||||
return createSequentialModifier(() => [
|
return createSequentialModifier(() => [
|
||||||
createAdditiveModifier(() => ({ ...options, addend: value })),
|
createAdditiveModifier(() => ({ ...(options ?? {}), addend: value })),
|
||||||
createMultiplicativeModifier(() => ({ ...options, multiplier: value })),
|
createMultiplicativeModifier(() => ({ ...(options ?? {}), multiplier: value })),
|
||||||
createExponentialModifier(() => ({ ...options, exponent: value }))
|
createExponentialModifier(() => ({ ...(options ?? {}), exponent: value }))
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue