How to Format Currency to 2 Decimals in TypeScript

Format currency to 2 decimals in TypeScript: Command Prompt output of Intl.NumberFormat with USD

You can format currency to 2 decimals in TypeScript with Intl.NumberFormat: pass style: ‘currency’ and currency: ‘USD’, and you get a string like $1,234.50 with the symbol, commas and two decimals: Every output on this page came from a real run with TypeScript 7.0.2 and Node.js v22.22.2, strict mode on. Format currency to 2 decimals … Read more >>

How to Convert Number to Boolean in TypeScript

Convert number to boolean in TypeScript: Command Prompt output of Boolean(), !! and n !== 0 for 0, -0, NaN and Infinity

The quickest way to convert number to boolean in TypeScript is Boolean(n) or !!n. Both return false for 0, -0 and NaN, and true for every other number, including negatives and Infinity: I ran each snippet below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the console output you see is real. We’ll … Read more >>

The require Keyword in TypeScript (import = require, TS2591, ESM)

require in TypeScript: Command Prompt output of a CommonJS file requiring an ES module in Node.js 22

The require keyword in TypeScript is really Node.js’s CommonJS require() function: it loads a module synchronously and returns its exports. In a .ts file it needs Node’s types, and it returns any, so prefer import x = require() or plain import: Every example here ran on TypeScript 7.0.2 and Node.js v22.22.2, compiled in strict mode. … Read more >>

How to Get the Decimal Part of a Number in TypeScript

TypeScript decimal part of a number: Command Prompt output comparing % 1, trunc and floor for negative numbers

You can get the decimal part of a number in TypeScript with the remainder operator: x % 1 returns everything after the decimal point as a number. For 24.75 that’s 0.75, and an integer gives 0: My test machine runs TypeScript 7.0.2 and Node.js v22.22.2; I built each file with strict checks on and copied … Read more >>

How to Find a Key by Value in a TypeScript Dictionary

Find key by value in a TypeScript dictionary: Command Prompt output of Object.keys with find

To find a key by value in a TypeScript dictionary, get the keys with Object.keys(), type them as keyof, and call find() to return the first key whose value matches. If nothing matches, you get undefined: Nothing below is pseudo-code: each lookup went through tsc –strict and then Node, using TypeScript 7.0.2 and Node.js v22.22.2, … Read more >>

ReferenceError: fetch is not defined in TypeScript (Fixed)

ReferenceError fetch is not defined in TypeScript: Command Prompt output from Node.js with and without global fetch

ReferenceError: fetch is not defined in TypeScript means your compiled JavaScript is running somewhere that has no global fetch, usually Node.js older than 18 or a Jest jsdom test. Upgrade Node, or load node-fetch first: Every example below ran on TypeScript 7.0.2 and Node.js v22.22.2 with strict mode, and I copied the output straight from … Read more >>

How to Get the First Element from a TypeScript Dictionary

Get first element from a TypeScript dictionary: Command Prompt output of a Map's first entry with iterator next()

To get the first element from a TypeScript dictionary, read index 0 of Object.entries() for a plain object or Record, and call .entries().next().value for a Map. Both give back undefined when the dictionary is empty: I ran all five examples with TypeScript 7.0.2 and Node.js v22.22.2 (strict mode on), so the outputs you see are … Read more >>

How to Round to 2 Decimals in TypeScript (Without the 1.005 Bug)

Round to 2 decimals in TypeScript: Command Prompt output of a roundTo helper fixing 1.005 and 4.015

The quickest way to round to 2 decimals in TypeScript is Math.round(value * 100) / 100, which returns a number. It breaks on halfway values like 1.005, though, so when every cent matters, shift the decimal point with exponent notation instead: I ran every snippet through TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so … Read more >>