TypeScript Simple Types
TypeScript Simple Types: Primitive Annotations That Matter
TypeScript supports every JavaScript primitive type: string, number, boolean, bigint, symbol, undefined, and null. Always write primitive type names in all lowercase (string, number, boolean), never capitalized wrapper classes like String or Number.
Unlike languages with separate int and float types, TypeScript follows JavaScript and uses number for integers, decimals, hexadecimal literals, and binary numbers alike.
Annotating Primitives in Real Code
You attach a type after a colon (:) on variables, parameters, or return values:
const productName: string = "Mechanical Keyboard";
const unitPrice: number = 89.99;
const inStock: boolean = true;
const ledgerId: bigint = 9007199254740993n;
function calculateSubtotal(price: number, qty: number): number {
return Number((price * qty).toFixed(2));
}
console.log(productName, inStock, ledgerId, calculateSubtotal(unitPrice, 2));
Notice bigint literals end with n and require "target": "ES2020" or newer in tsconfig.json.
Lowercase string vs Capitalized String
string is JavaScript's fast primitive text value, whereas String refers to the boxed wrapper object type. Using String breaks compatibility when passing values to functions expecting string.
Common Mistakes to Avoid
- Writing
String,Number, orBooleanas types: Always use lowercasestring,number, andboolean. - Mixing
numberandbigintin arithmetic:10 + 5nraises a TypeScript error because JavaScript forbids mixing them directly without explicitNumber(...)orBigInt(...). - Adding explicit types to every trivial constant:
const active = truealready infersboolean; save annotations for function parameters, return types, and empty declarations.
Frequently Asked Questions
Does TypeScript have separate int and float types?
No. JavaScript stores all standard numbers as IEEE-754 doubles, so TypeScript models both integers and decimals as number.
What is the symbol primitive type used for?
Symbol("key") creates guaranteed unique property identifiers (unique symbol) to avoid property name collisions on shared objects.
Why does const x = "hello" infer "hello" instead of string?
Because const cannot be reassigned, TypeScript narrows its type to the exact literal "hello", while let x = "hello" widens to string.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.