You are viewing a free preview of this lesson.
Subscribe to unlock all 12 lessons in this course and every other course on LearningBro.
TypeScript provides a rich set of built-in types that map closely to JavaScript's runtime values. Understanding these foundational types is essential before moving on to more advanced features. This lesson covers every primitive type, arrays, tuples, and the special types that TypeScript adds on top of JavaScript.
A type annotation tells the compiler what type a variable, parameter, or return value should be. You write a colon followed by the type after the identifier:
let username: string = 'Alice';
let age: number = 30;
let isActive: boolean = true;
TypeScript can often infer the type from the assigned value, so explicit annotations are optional in many cases. However, they are invaluable for function signatures and complex objects.
Represents textual data. Template literals are fully supported:
let greeting: string = 'Hello';
let message: string = `Welcome, ${greeting}`;
All numbers in TypeScript (and JavaScript) are floating-point values. There is no separate integer type:
let count: number = 42;
let price: number = 9.99;
let hex: number = 0xff;
let binary: number = 0b1010;
A simple true or false value:
let isDone: boolean = false;
These represent the absence of a value. With strictNullChecks enabled (recommended), they are not assignable to other types by default:
let nothing: null = null;
let notDefined: undefined = undefined;
// With strictNullChecks:
// let name: string = null; // Error!
Opts out of type checking entirely. Use sparingly — it defeats the purpose of TypeScript:
let flexible: any = 'hello';
flexible = 42; // No error
flexible = true; // No error
flexible.anything(); // No error — but will crash at runtime!
The type-safe counterpart to any. You must narrow the type before using the value:
let input: unknown = 'hello';
// input.toUpperCase(); // Error: Object is of type 'unknown'
if (typeof input === 'string') {
console.log(input.toUpperCase()); // OK — narrowed to string
}
Used as the return type of functions that do not return a value:
function logMessage(msg: string): void {
console.log(msg);
}
Represents values that never occur. Used for functions that always throw or have infinite loops:
Subscribe to continue reading
Get full access to this lesson and all 12 lessons in this course.