Terminology (7)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- number
- The primitive numeric type for ordinary TypeScript values, using IEEE 754 binary64. bigint is a separate numeric primitive.
- IEEE 754
- The floating-point standard behind number; binary64 has 53 significant bits.
- MAX_SAFE_INTEGER
- Number.MAX_SAFE_INTEGER, 2⁵³ − 1 = 9007199254740991, the largest integer number holds with no other integer rounding to it.
- Safe integer
- An integer that number represents exactly and uniquely; Number.isSafeInteger tests for it.
- bigint
- An arbitrary-precision integer primitive within implementation resource limits. Literals use an n suffix, such as 10n.
- BigInt()
- Parses integer text or converts a finite integer number. A number conversion preserves the represented integer, including any prior rounding; non-integral or non-finite numbers throw RangeError.
- Truncating division
- Division that drops the remainder toward zero; bigint / always does this.
Approximate numbers and exact integers
TypeScript’s number and bigint describe different runtime numeric types. number uses binary64 with finite precision; bigint represents integers exactly as they grow, subject to available resources. bigint does not represent fractional values, so an integer calculation still needs a policy for any remainder.
Conversion is a boundary between those representations. Decimal text retains its original digits until it is parsed; converting an already approximated number cannot reconstruct them. Follow the input representation, the arithmetic operands and the output format as one path.
01The safe range
Check the copied order ID against the safe range
The order ledger’s API sends IDs as decimal text from a 64-bit system. A copied test ID is now a TypeScript number literal; compare it with the source value before using it to assign the next order. After that input boundary, the ledger still needs to calculate whole-cent averages and send signed dollar balances back out.
The ledger’s first probe copies order ID 9007199254740993 into a numeric literal. TypeScript’s ordinary number type uses binary64. Predict what value this particular literal supplies to the first print, then check whether it is safe as an integer ID.
const orderId = 9007199254740993;
console.log(orderId);
console.log(Number.isSafeInteger(orderId), Number.MAX_SAFE_INTEGER);
npx tsc --noEmit --strict --target es2022 main.ts && node main.ts
Use the TypeScript setup from the first reading and replace the file for each experiment. bigint literals such as 1n need a target of ES2020 or later. This lesson is TypeScript-only: ordinary number values follow JavaScript’s binary64 semantics, while bigint is a separate numeric primitive type.
02From text
Convert text to bigint directly, not through number
The API sends that ID as decimal text, "9007199254740993". The ledger can choose its representation while those original digits are still available. Compare the three conversions and predict which lines keep the final 3.
const field = "9007199254740993";
console.log(Number(field));
console.log(BigInt(Number(field)));
console.log(BigInt(field));
03bigint arithmetic
Give the average a compatible count and a rounding policy
A customer’s orders total 1001n cents over 2 orders. The probe converts that count to bigint so division can proceed. The ledger needs an average in whole cents, so inspect what division keeps. Its second line uses a negative amount to establish the rule needed later for refunds. Predict that line.
const total = 1001n;
const count = 2;
console.log(total / BigInt(count), total % BigInt(count));
console.log(-7n / 2n, -7n % 2n);
04Crossing back
Converting a bigint to number can round again
The ledger’s export adapter can hand the same exact integer to another component as a number or as text. Compare both paths for 2n ** 53n + 1n, then inspect its comparison with Number.MAX_SAFE_INTEGER. Predict the first line.
const big = 2n ** 53n + 1n;
console.log(Number(big));
console.log(big.toString());
console.log(big > Number.MAX_SAFE_INTEGER);
05Repair the order ledger
Next ID, average order, then the balance
Continue the order ledger with an exact next ID, a whole-cent customer average and a signed dollar export. Average requests contain at least one nonnegative whole-cent order amount and round half a cent up. Balance exports may be negative. The readers supply these valid inputs; input validation is outside these repairs.
The lab opens in TypeScript. Each file contains its imports, reader and complete main. Later levels supply the earlier correct helpers.
Repair the order ledger
Exact parsing, bigint division that rounds on purpose, then formatting signed bigint amounts.
In the frontend Keep the order ID as text through the component boundary Optional
The API contract supplies the order ID as valid decimal text. The order link must preserve those digits in its label and destination. Both components receive that text from their parent; neither converts it to number.
type Props = { orderId: string };
export default function OrderLink({ orderId }: Props) {
return <a href={`/orders/${orderId}`}>Order {orderId}</a>;
}
React passes the string prop into both text positions without numeric conversion. The string annotation describes the supplied API contract; it does not validate a response at run time. If the API instead sends a large JSON numeric token, default JSON.parse may round it before this component receives it. String conversion afterward cannot recover the source digits.
Sources: React: passing props,Svelte: $props,ECMAScript: JSON.parse.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?