TypeScript Basics: Variables, Data Types, and Type Annotations
Course Title: Mastering TypeScript: From Basics to Advanced Applications Section Title: Introduction to TypeScript and Setup Topic: Basic syntax: variables, data types, and type annotations.
As we dive deeper into the world of TypeScript, it's essential to understand the fundamental building blocks of the language. In this topic, we'll explore the basic syntax of TypeScript, including variables, data types, and type annotations. These concepts are crucial for writing efficient and maintainable code.
Variables in TypeScript
In TypeScript, variables are declared using the let
, const
, or var
keywords. The main difference between these keywords is the scope and behavior of the variable.
let
: Thelet
keyword is used to declare block-scoped variables. This means that the variable is only accessible within the block it's declared in.const
: Theconst
keyword is used to declare constant variables. Variables declared withconst
cannot be reassigned.var
: Thevar
keyword is used to declare function-scoped variables. Variables declared withvar
are accessible throughout the function they're declared in.
Here's an example:
// let declaration
let name = 'John Doe';
// const declaration
const PI = 3.14159;
// var declaration
var age = 25;
Data Types in TypeScript
TypeScript supports the following data types:
- Number: The
number
type represents a numeric value. It can be an integer or a floating-point number. - String: The
string
type represents a sequence of characters. Strings can be enclosed in single quotes or double quotes. - Boolean: The
boolean
type represents a boolean value. It can be eithertrue
orfalse
. - Array: The
array
type represents a collection of values. - Null: The
null
type represents the absence of any object value. - Undefined: The
undefined
type represents an uninitialized variable or a symbol without a binding. - Tuple: The
tuple
type represents a fixed-length array. - Enum: The
enum
type represents a set of named values.
Here's an example of each data type:
// number
let age: number = 25;
// string
let name: string = 'John Doe';
// boolean
let isAdmin: boolean = false;
// array
let numbers: number[] = [1, 2, 3];
// null
let nullValue: null = null;
// undefined
let undefinedValue: undefined = undefined;
// tuple
let person: [string, number] = ['John Doe', 25];
// enum
enum Color {
Red,
Green,
Blue
}
let favoriteColor: Color = Color.Green;
Type Annotations
Type annotations in TypeScript are used to assign a specific type to a variable, function parameter, or return value. Type annotations are denoted by the :
symbol followed by the type.
Here are a few examples:
// variable type annotation
let name: string = 'John Doe';
// function parameter type annotation
function greet(name: string): void {
console.log(`Hello, ${name}!`);
}
// function return type annotation
function add(x: number, y: number): number {
return x + y;
}
Best practices for type annotations:
- Always include type annotations for variable declarations.
- Include type annotations for function parameters and return values.
- Use type annotations for properties in interfaces.
Practice Time!
- Declare a variable
username
with the typestring
and assign it the value'johnDoe'
. - Declare a variable
age
with the typenumber
and assign it the value25
. - Create an array of numbers and assign it to a variable
numbers
.
Resources:
If you have any questions or need further clarification, please leave a comment below and I'll be happy to help.
In the next topic, we'll explore how to compile TypeScript to JavaScript. This will include setting up the TypeScript compiler, understanding compiler options, and working with source maps.
Images

Comments