DEV Community

Jess Alejo
Jess Alejo

Posted on

Exploring TypeScript: Variable and Function Return Types

As a developer with a background in C# and Ruby, I’ve always appreciated clean, well-structured code. I’d heard of TypeScript for a while, mostly as a side mention in tutorials, but I never got around to exploring it. Now that it's part of a course I'm working on, I figured it was time to dive in and see what it offers. Here's a rundown of what I discovered while getting familiar with the key data types and features.


1. Basic Data Types: Back to the Basics

let isDone: boolean = true;
let age: number = 25;
let userName: string = "John Doe";
Enter fullscreen mode Exit fullscreen mode

Coming from JavaScript, it's a bit refreshing to declare types explicitly. Knowing whether I'm dealing with a number, boolean, or string upfront sounds like a win for debugging.


2. Arrays: No More Guesswork

let numberArray: number[] = [1, 2, 3];
let stringArray: string[] = ["one", "two", "three"];
let mixedArray: (number | string)[] = [1, "two", 3];
Enter fullscreen mode Exit fullscreen mode

With TypeScript, I can define what goes into my arrays—and avoid surprises when iterating through them.


3. Tuples: Organized Mixed Data

let tuple: [number, string] = [1, "Hello"];
Enter fullscreen mode Exit fullscreen mode

Tuples look pretty handy when I want to store mixed data types but keep them in a specific order.


4. Enums: Cleaner Code with Named Constants

enum Color {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}
let favoriteColor: Color = Color.Green;
Enter fullscreen mode Exit fullscreen mode

Enums feel like a great way to reduce magic values floating around my code.


5. Any: A Double-Edged Sword

let anything: any = "Could be anything";
Enter fullscreen mode Exit fullscreen mode

any might sound freeing at first, but it seems like it defeats the purpose of TypeScript. I get why it's better to use it sparingly.


6. Void: For Functions That Don't Return Values

function logMessage(message: string): void {
  console.log(message);
}
Enter fullscreen mode Exit fullscreen mode

Explicitly marking functions that don't return anything? I can get behind that—it makes things clearer.


7. Null and Undefined: Separate and Intentional

let n: null = null;
let u: undefined = undefined;
Enter fullscreen mode Exit fullscreen mode

JavaScript has given me my fair share of headaches with null and undefined. TypeScript making me handle these intentionally sounds promising.


8. Never: For Functions That Don't End Well

function throwError(message: string): never {
  throw new Error(message);
}
Enter fullscreen mode Exit fullscreen mode

I didn’t realize never existed, but it makes sense for functions that either throw errors or never return.


9. Objects: Clearer Structures

let person: object = { name: "John", age: 30 };
Enter fullscreen mode Exit fullscreen mode

It's nice to define objects upfront and know what properties to expect.


10. Union Types: Flexibility Without the Chaos

let unionType: number | string = "Can be a string or number";
Enter fullscreen mode Exit fullscreen mode

Union types give flexibility while keeping things predictable—a happy middle ground.


11. Literal Types: Being Super Specific

let specificValue: "Hello" | "Goodbye" = "Hello";
Enter fullscreen mode Exit fullscreen mode

This seems useful when I want to limit a variable to specific values, like user roles or statuses.


12. Function Types: Typing Function Logic

let add: (x: number, y: number) => number;
add = (x: number, y: number) => x + y;
Enter fullscreen mode Exit fullscreen mode

Defining what goes in and out of a function makes my code more predictable.


13. Interfaces: Structure Without Chaos

interface Person {
  name: string;
  age: number;
}
let personDetails: Person = { name: "Alice", age: 25 };
Enter fullscreen mode Exit fullscreen mode

Interfaces seem like a great way to manage consistent object structures, especially in larger codebases.


14. Type Aliases: Cleaner Definitions

type Point = { x: number; y: number };
let point: Point = { x: 10, y: 20 };
Enter fullscreen mode Exit fullscreen mode

I like the idea of giving complex types readable names—it makes the code cleaner.


15. Type Assertions: Taking Control

let someValue: any = "Hello TypeScript";
let strLength: number = (someValue as string).length;
Enter fullscreen mode Exit fullscreen mode

Sometimes, I know better than TypeScript. Assertions give me that control when I need it.


16. Generics: Reusable Functions Done Right

function identity<T>(arg: T): T {
  return arg;
}
let output = identity<string>("Generics are fun");
Enter fullscreen mode Exit fullscreen mode

Generics feel like a cheat code for writing reusable and type-safe functions.


17. Intersection Types: Merging the Best of Both Worlds

type A = { a: string };
type B = { b: number };
type AB = A & B;
let intersectedValue: AB = { a: "Hello", b: 42 };
Enter fullscreen mode Exit fullscreen mode

This seems really useful for combining multiple object types into one.


18. Optional Properties: Flexibility for Object Definitions

interface UserProfile {
  username: string;
  bio?: string;
}
let userProfile: UserProfile = { username: "coder123" };
Enter fullscreen mode Exit fullscreen mode

Optional properties are great when you don’t always have all the information up front.


19. Type Literals: Custom Key-Value Structures

type KeyValue = Record<string, number>;
let keyValueStore: KeyValue = { apples: 10, oranges: 15 };
Enter fullscreen mode Exit fullscreen mode

20. Type Predicates: Custom Type Checks

function isString(value: any): value is string {
  return typeof value === "string";
}
if (isString("test")) {
  console.log("It's a string!");
}
Enter fullscreen mode Exit fullscreen mode

Type predicates let me be explicit when checking custom conditions.


Final Thoughts

So far, TypeScript has been a pleasant surprise. It brings the structured, maintainable approach while still feeling flexible enough to accommodate different coding styles. And honestly, it feels way less chaotic than JavaScript's wild west.

Top comments (0)