TypeScript Simple Types: The Basics

Welcome to the world of TypeScript simple types! These are the building blocks for creating powerful, statically-typed JavaScript programs. Let’s take a look at some of the most common simple types you’ll use:

  • boolean: represents a true or false value, perfect for when you need to make a decision (like if it’s time for lunch or not).
  • number: represents integers or floating-point values. This is your go-to type for working with numbers, whether you’re doing math or just counting your steps.
  • string: represents a sequence of characters, so you can use it to store things like names, addresses, and your favorite emoji.
  • null: represents the intentional absence of an object value. This is useful when you want to explicitly indicate that something is missing.
  • undefined: represents the absence of a value. This is different from null, which represents the absence of an object value.

Finally, there’s the any type, which can be used to represent any value. This is great for situations where you don’t know the type of a value upfront, or when you need to change the type at runtime.

Here’s how you might use these types in a TypeScript program:

let isDone: boolean = false;
let age: number = 30;
let name: string = "John";
let nothing: null = null;
let value: undefined = undefined;
let something: any = "hello";

And that’s it! With these simple types in your toolbox, you’re ready to start creating awesome, statically-typed programs in TypeScript. Happy coding!