Tillitsdone
down Scroll to discover

Understanding TypeScript Types: Basics & Examples

Dive into TypeScript's fundamental types system, from basic primitives to custom types.

Learn how to leverage TypeScript's type checking for more reliable and maintainable JavaScript code.
thumbnail

Understanding TypeScript Types: Basics and Examples

Abstract flowing patterns resembling code structures featuring bright zinc metallic and stone blue colors intertwining in smooth waves shot from directly above high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

TypeScript has revolutionized the way we write JavaScript by introducing a powerful type system. As a developer who’s spent countless hours debugging JavaScript code, I can’t emphasize enough how TypeScript has made my life easier. Let’s dive into the fundamental types that make TypeScript so valuable.

The Building Blocks: Basic Types

When I first started with TypeScript, understanding the basic types was my foundation. These are the simplest yet most crucial elements you’ll work with:

Number

In TypeScript, all numbers, whether integers or decimals, are represented by the number type. Here’s how I use them in my daily coding:

let price: number = 99.99;
let quantity: number = 42;
let temperature: number = -5;

Aerial view of winding river paths through bright yellow desert landscape creating natural flowing patterns camera angle from directly above at high altitude high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

String

Strings are probably the most commonly used type in any programming language. In TypeScript, we can be explicit about our string declarations:

let firstName: string = "John";
let message: string = `Hello, ${firstName}`;

Boolean

The boolean type might seem simple, but it’s incredible how much clearer your code becomes when you explicitly declare boolean variables:

let isActive: boolean = true;
let isLoggedIn: boolean = false;

Working with Arrays and Tuples

Arrays are where TypeScript really starts to shine. I remember how many bugs I used to encounter with JavaScript arrays - TypeScript makes those a thing of the past:

let numbers: number[] = [1, 2, 3, 4, 5];
let fruits: Array<string> = ["apple", "banana", "orange"];

Macro shot of seaweed formations in ocean creating abstract patterns rich maroon and seaweed green colors camera angle from side view with slight tilt high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

The Power of Custom Types

One of my favorite features in TypeScript is the ability to create custom types. It’s like having a blueprint for your data:

type User = {
id: number;
name: string;
email: string;
isAdmin: boolean;
};
let currentUser: User = {
id: 1,
name: "John Doe",
email: "john@example.com",
isAdmin: false
};

Union Types: Flexibility When You Need It

Sometimes we need variables that can hold different types of values. Union types are TypeScript’s elegant solution to this:

let statusCode: number | string = 200;
statusCode = "OK"; // This is also valid

Remember, TypeScript’s type system is your friend. It might feel like extra work at first, but trust me - the peace of mind and productivity boost you get from catching errors at compile-time instead of runtime is absolutely worth it.

Abstract geometric patterns in bright green and black colors viewed from a 45-degree angle featuring sharp angular shapes and clean lines high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Conclusion

As you continue your TypeScript journey, you’ll discover that these basic types are just the beginning. The type system is incredibly rich and flexible, allowing you to express complex relationships between data in your applications. Start with these fundamentals, and you’ll be well on your way to writing more reliable and maintainable code.

icons/code-outline.svg Typescript Blogs
Superset of JavaScript adding static types for improved code quality and maintainability.
icons/logo-tid.svg
Talk with CEO
Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.