Made modifier constructors have more explicit typing

This commit is contained in:
thepaperpilot 2022-05-01 17:25:10 -05:00
parent 22f3415069
commit b7789563e2

View file

@ -34,23 +34,34 @@ export interface Modifier {
description?: ProcessedComputable<CoercableComponent>; description?: ProcessedComputable<CoercableComponent>;
} }
/**
* 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 ModifierFromOptionalParams<T, S> = T extends undefined
? S extends undefined
? Omit<WithRequired<Modifier, "revert">, "description" | "enabled">
: Omit<WithRequired<Modifier, "revert" | "enabled">, "description">
: S extends undefined
? Omit<WithRequired<Modifier, "revert" | "description">, "enabled">
: WithRequired<Modifier, "revert" | "enabled" | "description">;
/** /**
* Create a modifier that adds some value to the input value * Create a modifier that adds some value to the input value
* @param addend The amount to add to the input value * @param addend The amount to add to the input value
* @param description Description of what this modifier is doing * @param description Description of what this modifier is doing
* @param enabled A computable that will be processed and passed directly into the returned modifier * @param enabled A computable that will be processed and passed directly into the returned modifier
*/ */
export function createAdditiveModifier( export function createAdditiveModifier<
addend: Computable<DecimalSource>, T extends Computable<CoercableComponent> | undefined,
description?: Computable<CoercableComponent>, S extends Computable<boolean> | undefined,
enabled?: Computable<boolean> R = ModifierFromOptionalParams<T, S>
): Modifier { >(addend: Computable<DecimalSource>, description?: T, enabled?: S): R {
const processedAddend = convertComputable(addend); const processedAddend = convertComputable(addend);
const processedDescription = convertComputable(description); const processedDescription = convertComputable(description);
const processedEnabled = enabled == null ? undefined : convertComputable(enabled); const processedEnabled = enabled == null ? undefined : convertComputable(enabled);
return { return {
apply: gain => Decimal.add(gain, unref(processedAddend)), apply: (gain: DecimalSource) => Decimal.add(gain, unref(processedAddend)),
revert: gain => Decimal.sub(gain, unref(processedAddend)), revert: (gain: DecimalSource) => Decimal.sub(gain, unref(processedAddend)),
enabled: processedEnabled, enabled: processedEnabled,
description: description:
description == null description == null
@ -66,7 +77,7 @@ export function createAdditiveModifier(
) : null} ) : null}
</div> </div>
)) ))
}; } as unknown as R;
} }
/** /**
@ -75,17 +86,17 @@ export function createAdditiveModifier(
* @param description Description of what this modifier is doing * @param description Description of what this modifier is doing
* @param enabled A computable that will be processed and passed directly into the returned modifier * @param enabled A computable that will be processed and passed directly into the returned modifier
*/ */
export function createMultiplicativeModifier( export function createMultiplicativeModifier<
multiplier: Computable<DecimalSource>, T extends Computable<CoercableComponent> | undefined,
description?: Computable<CoercableComponent>, S extends Computable<boolean> | undefined,
enabled?: Computable<boolean> R = ModifierFromOptionalParams<T, S>
): Modifier { >(multiplier: Computable<DecimalSource>, description?: T, enabled?: S): R {
const processedMultiplier = convertComputable(multiplier); const processedMultiplier = convertComputable(multiplier);
const processedDescription = convertComputable(description); const processedDescription = convertComputable(description);
const processedEnabled = enabled == null ? undefined : convertComputable(enabled); const processedEnabled = enabled == null ? undefined : convertComputable(enabled);
return { return {
apply: gain => Decimal.times(gain, unref(processedMultiplier)), apply: (gain: DecimalSource) => Decimal.times(gain, unref(processedMultiplier)),
revert: gain => Decimal.div(gain, unref(processedMultiplier)), revert: (gain: DecimalSource) => Decimal.div(gain, unref(processedMultiplier)),
enabled: processedEnabled, enabled: processedEnabled,
description: description:
description == null description == null
@ -101,7 +112,7 @@ export function createMultiplicativeModifier(
) : null} ) : null}
</div> </div>
)) ))
}; } as unknown as R;
} }
/** /**
@ -110,17 +121,17 @@ export function createMultiplicativeModifier(
* @param description Description of what this modifier is doing * @param description Description of what this modifier is doing
* @param enabled A computable that will be processed and passed directly into the returned modifier * @param enabled A computable that will be processed and passed directly into the returned modifier
*/ */
export function createExponentialModifier( export function createExponentialModifier<
exponent: Computable<DecimalSource>, T extends Computable<CoercableComponent> | undefined,
description?: Computable<CoercableComponent>, S extends Computable<boolean> | undefined,
enabled?: Computable<boolean> R = ModifierFromOptionalParams<T, S>
): Modifier { >(exponent: Computable<DecimalSource>, description?: T, enabled?: S): R {
const processedExponent = convertComputable(exponent); const processedExponent = convertComputable(exponent);
const processedDescription = convertComputable(description); const processedDescription = convertComputable(description);
const processedEnabled = enabled == null ? undefined : convertComputable(enabled); const processedEnabled = enabled == null ? undefined : convertComputable(enabled);
return { return {
apply: gain => Decimal.pow(gain, unref(processedExponent)), apply: (gain: DecimalSource) => Decimal.pow(gain, unref(processedExponent)),
revert: gain => Decimal.root(gain, unref(processedExponent)), revert: (gain: DecimalSource) => Decimal.root(gain, unref(processedExponent)),
enabled: processedEnabled, enabled: processedEnabled,
description: description:
description == null description == null
@ -136,7 +147,7 @@ export function createExponentialModifier(
) : null} ) : null}
</div> </div>
)) ))
}; } as unknown as R;
} }
/** /**
@ -146,16 +157,19 @@ export function createExponentialModifier(
* @param modifiers The modifiers to perform sequentially * @param modifiers The modifiers to perform sequentially
* @see {@link createModifierSection} * @see {@link createModifierSection}
*/ */
export function createSequentialModifier( export function createSequentialModifier<
...modifiers: Modifier[] T extends Modifier[],
): WithRequired<Modifier, "description"> { S = T extends WithRequired<Modifier, "revert">[]
? WithRequired<Modifier, "description" | "revert">
: Omit<WithRequired<Modifier, "description">, "revert">
>(...modifiers: T): S {
return { return {
apply: gain => apply: (gain: DecimalSource) =>
modifiers modifiers
.filter(m => unref(m.enabled) !== false) .filter(m => unref(m.enabled) !== false)
.reduce((gain, modifier) => modifier.apply(gain), gain), .reduce((gain, modifier) => modifier.apply(gain), gain),
revert: modifiers.every(m => m.revert != null) revert: modifiers.every(m => m.revert != null)
? gain => ? (gain: DecimalSource) =>
modifiers modifiers
.filter(m => unref(m.enabled) !== false) .filter(m => unref(m.enabled) !== false)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@ -172,7 +186,7 @@ export function createSequentialModifier(
).map(renderJSX)} ).map(renderJSX)}
</> </>
)) ))
}; } as unknown as S;
} }
/** /**