Primitive type annotations
Concept and syntax
Use lowercase string, number and boolean for ordinary text, numeric values and true/false values. An annotation follows a variable name and constrains later assignments. number includes fractional values; it does not mean integer. Uppercase String, Number and Boolean describe wrapper-related types and are not the normal annotation for primitives. Keep booleans as actual true/false values, not their textual spellings.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
let labelText: string = "tea";let price: number = 2.5;let ready: boolean = true;console.log(labelText, price * 2, ready);Expected output (one line per log):
tea 5 trueCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
Expected compiler diagnostic: TS2322. Text "true" is not the boolean true. Use a boolean literal or compare the string explicitly.
Practice and recap
Declare a numeric discount, a string label and a boolean flag. Try assigning a numeric-looking string to the discount.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Primitive type annotations — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which annotation fits a price of 2.5?
Which assignment fits boolean?
Which initializer is valid for ready?
let ready: boolean = "true";