Static Typing in TypeScript: Typescript Basics

Understanding Static Typing

Patrick Karsh
3 min readSep 20, 2023

TypeScript, a super-set of JavaScript, was developed by Microsoft in 2012 to address some of the limitations of JavaScript. One of its primary offerings is its optional static type system. While JavaScript is a dynamically typed language, where variable types are not set until runtime, TypeScript offers developers the opportunity to specify types at compile-time through static typing.

Understanding Static Typing

Static typing means that the type of a variable is known at compile-time instead of at runtime. For instance, once a variable is declared to be of a certain type in a statically typed language, it cannot be reassigned to a different type later. This can prevent many common errors that can occur in dynamically typed languages where the type of a variable can change during the execution of the program.

Here’s a simple example in TypeScript:

let age: number = 25;

In the above code, the variable `age` is declared with the type `number`. If we try to assign a string or any other type to `age`, TypeScript will throw a compile-time error.

Benefits of Static Typing

  • Error Detection: One of the primary benefits of static typing is early error detection. TypeScript’s compiler checks the types and provides immediate feedback on type mismatches or other type-related errors. This can save developers a significant amount of time debugging runtime errors that might have been overlooked in a dynamically typed setting.
  • Readability: By explicitly declaring types, the code often becomes more readable. Future maintainers of the code can quickly understand the expected data structures and types in use.
  • Documentation: Types can serve as a form of documentation. Instead of relying on comments to convey the expected type of a variable or the return type of a function, the type annotations in the code provide this information.
  • Tooling and Refactoring: Integrated Development Environments (IDEs) can leverage TypeScript type annotations to provide intelligent code suggestions, autocompletion, and more effective refactoring tools.

Generics and Advanced Types

Apart from basic static typing, TypeScript offers advanced type features like generics, union types, and intersection types. These provide developers with a flexible and robust system for defining types.

For instance, generics allow for creating reusable components with a variable type:

function echo<T>(item: T): T {
return item;
}

Here, `echo` is a generic function that can work with any type, ensuring that the input type matches the output type.

Inference and Any

TypeScript is smart about type inference. If you don’t provide a type, TypeScript will infer it based on the initial value. For example:

let name = “Alice”; // TypeScript infers the type as ‘string’

However, there are scenarios where a developer might not want to specify a type, or the type might be too complex to annotate. For these cases, TypeScript offers the `any` type:

let data: any = “This could be anything”;
data = 42; // No error here

While the `any` type offers flexibility, it bypasses type-checking and should be used judiciously.

Interfaces and Type Aliases

TypeScript introduces the concept of interfaces and type aliases to define the shape of an object. With interfaces, you can specify the contract that certain objects should adhere to:

interface Person {
firstName: string;
lastName: string;
age?: number; // Optional property
}

Type aliases, on the other hand, allow naming a type, which can be useful for union types, tuple types, and more:

type StringOrNumber = string | number;

Conclusion

Static typing in TypeScript provides a plethora of advantages to developers, ranging from error prevention to enhanced readability and tooling support. While introducing a learning curve for those accustomed to JavaScript’s dynamically typed nature, the benefits of adopting TypeScript’s static type system often outweigh the initial ramp-up time.

By harnessing the power of static types, developers can write more robust, maintainable, and scalable applications, ensuring that they catch potential issues early in the development lifecycle.

--

--

Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh