Reading TypeScript
A 5-minute primer on reading the TypeScript used in pricing equations - enough to understand what an equation does and tweak the numbers.
A 5-minute primer. You don't need to write TypeScript - just read it well enough to understand what an equation is doing and tweak the numbers.
Constants and variables
const NOZZLE_DIAMETER = 0.4 // a constant — this value never changes in the equation
let totalCost = 0 // a variable — can be reassigned laterIn practice, pricing equations use const for almost everything. You rarely need let.
Numbers and arithmetic
const materialCost = massKg * pricePerKg // multiplication
const setupPerPart = setupFee / quantity // division
const total = materialCost + machineCost + setupPerPart // addition
const discounted = total * 0.90 // 10% discount (multiply by 0.90)Conditions
if (length > 300) {
// this block runs when the part is longer than 300mm
} else {
// this block runs otherwise
}Conditions inside expressions:
const surcharge = quantity < 5 ? 1.20 : 1.00
// reads as: "if quantity < 5, use 1.20; otherwise use 1.00"Destructuring
A shorthand for pulling multiple values out of an object:
// Long form:
const volume = specification.volume
const area = specification.area
const length = specification.length
// Shorthand (same result):
const { volume, area, length } = specificationYou'll see this at the top of almost every equation.
Optional chaining and fallbacks
Some values might not exist (the customer didn't set them). Two operators handle this:
precision?.value
// means: "get precision.value if precision exists; otherwise return undefined"
precision?.value ?? 0.20
// means: "get precision.value if it exists; if not (or if it's null), use 0.20"You'll see this pattern a lot for infill?.value ?? 0.20 and precision?.value ?? 0.20.
Functions
function selectMachine(length: number, width: number): string {
if (length <= 250 && width <= 250) return 'Bambu X1 Carbon'
// Else
return 'Creality K1 Max'
}
// Call it like this:
const machine = selectMachine(length, width)The part after the colon (: number, : string) is just a type label - it tells you what kind of value to expect. Ignore it when reading; it doesn't affect the logic.
Math functions
Math.max(5.00, calculatedPrice) // returns the larger of the two
Math.min(500, calculatedPrice) // returns the smaller of the two
Math.sqrt(quantity) // square root
Math.log(quantity + 1) // natural logarithm
Math.pow(base, exponent) // base to the power of exponent
Math.ceil(3.2) // rounds up → 4
Math.floor(3.8) // rounds down → 3Arrays and loops
const MACHINES = [
{ name: 'Small', maxDim: 250, costPerHour: 6 },
{ name: 'Large', maxDim: 500, costPerHour: 12 },
]
// Find the first machine that fits:
const machine = MACHINES.find(m => Math.max(length, width, height) <= m.maxDim)find() goes through the array and returns the first item where the condition is true.
That's all the TypeScript you need. If you see something unfamiliar in an equation, it's almost certainly a variation of one of these patterns.
Last updated on