Primitive Types

The everyday primitive types are string, number, boolean, bigint, symbol, and the special types null and undefined (with nullability behavior governed by compiler options). Use lowercase type names such as string, not the JavaScript wrapper object type String.

TypeScript also supports void for functions whose return value is not intended to be used, never for values that cannot occur, and object for non-primitive values. Avoid Object as a broad type because it does not express a useful data contract.

Primitive annotations
const username: string = "Mira";
const retryCount: number = 3;
const isEnabled: boolean = true;

function logMessage(message: string): void {
  console.log(message);
}
💡 The annotations express the intended values. In many cases the compiler can infer the types, so explicit annotations are most useful at public boundaries, function parameters, and places where inference would be unclear.
Key Takeaways
  • Use lowercase primitive type names.
  • void describes an intentionally unused return value.
  • Avoid broad wrapper types such as String and Object.
Knowledge Check

Which is the recommended TypeScript primitive type annotation?