FOUNDATIONS · LESSON 2 OF 24
Primitive types and inference
Choose useful annotations while letting the compiler infer obvious values.
- string
- number
- boolean
- inference
Your runnable checkpoint
Extract into a new practice folder. Each download includes complete source, package.json, tsconfig.json and an offline lesson. Use Node.js 22 or 24. Each checkpoint is independent.
Install with npm install. Run npm run check, then npm run build. Terminal lessons use npm start; browser lessons use npm run serve and http://127.0.0.1:4173.
1. Understand the concepts
Use lowercase string, number and boolean for primitive values. number includes integers and decimals; it does not guarantee a positive amount or exclude NaN. Validate business rules separately.
Inference derives a type from an initializer or surrounding expression. let quantity = 2 is number because the binding can change. const currency = "INR" retains the literal type "INR" because that binding cannot be reassigned.
Annotate boundaries such as function parameters or exported data contracts. Repeating an obvious type on every local variable adds noise. A wider annotation may be intentional when a variable will hold several permitted values.
Template strings produce text. Arithmetic stays numeric until formatted. Here the values are simple teaching amounts; real money calculations should use a deliberate integer-minor-unit or decimal strategy.
2. Build and run
These are complete files. Preserve the paths shown below, including the src directory. The package and compiler configuration are included at the end.
src/main.ts
let quantity = 2;
const unitPrice = 150;
const currency = "INR";
const available: boolean = true;
const total = quantity * unitPrice;
console.log(`${currency} ${total} | available: ${available}`);
quantity += 1;
console.log(quantity);
package.json
{
"name": "edupoly-typescript-primitive-types-and-inference",
"private": true,
"version": "1.0.0",
"type": "module",
"scripts": {
"check": "tsc --noEmit",
"build": "tsc",
"start": "node dist/main.js"
},
"devDependencies": {
"typescript": "5.9.3"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": [
"ES2022",
"DOM"
],
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noEmitOnError": true,
"verbatimModuleSyntax": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"types": [],
"moduleDetection": "force"
},
"include": [
"src/**/*"
]
}
3. Trace the code and check the result
- quantity and unitPrice are inferred as numbers.
- Multiplication returns a number; interpolation converts it for display.
- const prevents rebinding, not deep mutation of an object.
Expected result
INR 300 | available: true
34. Practice independently
Create a function that accepts a price and quantity and returns their numeric total.
Show the explanation
Use function total(price: number, quantity: number): number { return price * quantity; }. Check total(150, 2) is 300; total("150", 2) must fail type checking.
When something goes wrong
If tsc is missing, run npm install in the folder containing package.json. If the output looks old, rebuild before running. Read the first compiler diagnostic and check the indicated field or argument. For module errors, preserve the .js extensions in imports. Browser examples must run through the included local server, not directly as TypeScript files.
