TypeScript Fundamentals

Sean Marcus
6 min readDec 13, 2022

--

TypeScript is a programming language that is a superset of JavaScript, meaning that all JavaScript code is also valid TypeScript code. However, TypeScript adds additional features to JavaScript, such as static typing and classes, which can help to make your code more predictable and easier to debug.

One of the key features of TypeScript is its support for static typing. This means that you can specify the type of a variable when you declare it, and the TypeScript compiler will check your code to make sure that you are using the variable consistently. For example, you might declare a variable like this:

let x: number = 5;

In this code, the : number part indicates that the x variable is of type number. This means that you can only assign number values to x, and the TypeScript compiler will give you an error if you try to assign a different type of value.

Another important feature of TypeScript is its support for classes. In TypeScript, you can use the class keyword to define a class, which is a template for creating objects. For example, you might define a Person class like this:

class Person {
constructor(public name: string, public age: number) {}
}

This class has two member variables: name and age, which are both of type string and number, respectively. The constructor function is used to create new objects from the class, and the public keyword indicates that these member variables should be accessible from outside the class.

To use the Person class, you can create a new object like this:

let person = new Person("John", 30);

This creates a new Person object with the name "John" and the age 30. You can then access the object's member variables using the dot notation, like this:

console.log(person.name);  // Output: "John"
console.log(person.age); // Output: 30

Overall, TypeScript is a powerful language that can help you to write more robust and maintainable code. By using static typing and classes, you can catch errors early and make your code easier to understand and work with.

Generics

TypeScript generics are a way to provide flexibility in your type declarations. Generics allow you to create reusable components that can work with multiple types, instead of being tied to a specific type.

For example, suppose you want to create a function that takes an array of values and returns the first element of the array. Without using generics, you might write a function like this:

function getFirst(arr: number[]) {
return arr[0];
}

This function is only able to work with arrays of numbers, because it has a type signature that specifies that it takes an array of numbers (number[]) and returns a number. If you try to pass an array of strings or any other type to this function, you will get an error.

To make the function more flexible, you can use generics to specify that it can work with arrays of any type. You do this by using the <T> syntax, where T is a placeholder for the type that will be specified when the function is called. Here's how the function would look with generics:

function getFirst<T>(arr: T[]) {
return arr[0];
}

Now, when you call the getFirst function, you can specify the type that you want to use. For example, you can call the function with an array of numbers like this:

let arr = [1, 2, 3];
let first = getFirst<number>(arr); // first has the type number

Or you can call the function with an array of strings like this:

let arr = ["a", "b", "c"];
let first = getFirst<string>(arr); // first has the type string

In both cases, the first variable will have the correct type based on the type that you specified when you called the function.

Overall, TypeScript generics are a useful tool that can help you to write more flexible and reusable code. By using generics, you can create components that can work with multiple types, instead of being tied to a specific type. This can make your code more modular and easier to maintain.

Interfaces and Types

TypeScript interfaces and types are two related but distinct concepts.

TypeScript interfaces are a way to describe the shape of an object. An interface defines a set of properties and methods that an object must have in order to implement the interface. For example, you might define an interface like this:

interface Person {
name: string;
age: number;
greet(): string;
}

This interface defines a Person type that has a name property of type string, an age property of type number, and a greet method that returns a string. Any object that implements this interface must have these properties and methods, and must have them in the same way (i.e., with the same names, types, and return types).

Once you have defined an interface, you can use it to create objects that implement the interface. For example, you might define a Person class like this:

class Person implements Person {
constructor(public name: string, public age: number) {}
greet() {
return `Hello, my name is ${this.name}`;
}
}

This class implements the Person interface by defining the name, age, and greet properties and methods in the same way as the interface. You can then create objects from this class and use them as if they were Person objects:

let person = new Person("John", 30);
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 30
console.log(person.greet()); // Output: "Hello, my name is John"

On the other hand, TypeScript types are a way to create custom type definitions. A type can be used to specify the allowed values for a variable, the shape of an object, or the signature of a function. For example, you might define a type like this:

type Person = {
name: string;
age: number;
greet(): string;
}

This type is similar to the Person interface that we defined earlier, but it uses the type keyword instead of the interface keyword. The effect is the same: we have defined a Person type that has a name property of type string, an age property of type number, and a greet method that returns a string.

Once you have defined a type, you can use it in the same way that you would use an interface. For example, you could create a Person class like this:

class Person {
constructor(public name: string, public age: number) {}
greet() {
return `Hello, my name is ${this.name}`;
}
}

And then you could create an object from this class and assign it to a variable of type Person like this:

let person: Person = new Person("John", 30);
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 30
console.log(person.greet()); // Output: "Hello, my name is John"

In general, both interfaces and types are useful tools in TypeScript. Interfaces are a way

In Summary

TypeScript adds additional features to JavaScript, such as static typing and classes, which can help to make your code more predictable and easier to debug.

Some of the key benefits of using TypeScript include:

  • Improved code predictability and reliability: TypeScript’s static typing system allows you to catch errors early and prevent type-related bugs.
  • Better code organization and reuse: TypeScript’s support for classes and interfaces makes it easier to structure your code in a modular way and create reusable components.
  • Enhanced development experience: TypeScript integrates with popular code editors and IDEs, providing intelligent code completion, error checking, and other helpful features.
  • Stronger type checking: TypeScript’s type system is more powerful than JavaScript’s, allowing you to catch more type-related errors and write more precise and predictable code.
  • Easier migration from other languages: TypeScript’s syntax is similar to other popular languages like C# and Java, making it easier for developers from those languages to learn and use TypeScript.

Overall, TypeScript can be a valuable addition to any JavaScript project, providing a range of benefits that can help to improve the quality, predictability, and maintainability of your code.

--

--

Sean Marcus
Sean Marcus

No responses yet