As I embark on my journey to learn TypeScript, a statically typed superset of JavaScript, I’m excited to share my newfound knowledge about typescript.

Let Typescript be your guide

In my TypeScript learning adventure, I’ve discovered the magic of type inference. TypeScript’s ability to automatically deduce the types of variables, parameters, and return values based on context is simply fascinating. Here’s an example:

let age = 25; // TypeScript determines 'age' as a number`

I’ve realized that type inference can save me from having to write explicit type annotations when TypeScript can intelligently guess the right type.

Be explicit with Type Annotations

While type inference is impressive, I’ve also learned the importance of type annotations. Type annotations allow me to be explicit about types and provide an extra layer of type safety.

let name: string = "John";
function add(x: number, y: number): number {
    return x + y;
}

I’ve found that using type annotations is beneficial when I want to communicate my intentions clearly or when TypeScript’s inference doesn’t align with my thinking.

Exporting types in modules is important for sharing type definitions with other modules or external users. In the module, you can just type in this.

// types.ts
export type Person = {
    name: string;
    age: number;
};

By exporting the Person type, I make it accessible to other modules that import the file types.ts

Sometimes, there’s no need to export types. When a type is meant for internal use within a module and isn’t relevant outside. TypeScript still checks types effectively.

// myModule.ts
type InternalType = {
    // Internal type definition
};

function processData(data: InternalType) {
    // ...
}

I keep the InternalType unexported when it is only needed in the same module.

Does order matters ?

In TypeScript, when you define custom types using interfaces, type aliases, or classes, the order of the properties or members generally does not matter. These types are structural and rely on the shape or structure of the object rather than the order in which the properties or members are declared.

For instance, consider two types:

type PersonA = {
    name: string;
    age: number;
};

type PersonB = {
    age: number;
    name: string;
};

In this example, I defined two type aliases with the same properties but in a different order. TypeScript treats these types as equivalent because their shapes are the same. You can freely assign objects of one type to a variable of the other type without any issues:

const personA: PersonA = {
    name: "Alice",
    age: 30
};

const personB: PersonB = personA; // No type error

Tuple types

I also discovered that there are tuple types. Tuple types in TypeScript allow you to represent an array where the type of a fixed number of elements is known, and those elements can have different types. They provide a way to express a specific structure of data where the order of elements matters. Tuple types are essentially an array with a fixed number of elements, and each element has a predefined type.

Here’s how you define a tuple type in TypeScript:

let exampleTuple: [string, number] = ["hello", 42];

In the above example, the first element is of type string and the second is a number. This means that the tuple must always contain a string as the first element and a number as the second element, in that order.

You can access elements in a tuple by index, just like regular arrays.

let firstItem: string = exampleTuple[0];
let secondItem: number = exampleTuple[1];

Tuple types are useful when you need to work with data structures where the types and the order of elements are well-defined. For example, you might use tuple types to represent coordinates (latitude and longitude), key-value pairs, or any situation where you have a fixed set of different types that need to be stored together.

To end

My journey to learn TypeScript is still ongoing. I look forward to more learning and sharing on this incredible journey.

Resources