TypeScript

TypeScript and Static Typing: Explicit, Inferential, and Structured Approaches

TypeScript (TS) has become one of the most popular programming languages in modern web development. Its biggest advantage over plain JavaScript is static typing — a feature that allows developers to write more predictable, maintainable, and error-resistant code. In this article, we will explore what static typing is in TypeScript, how explicit and inferential typing works, and why structured typing is at the core of the language.


What is Static Typing in TypeScript?

Static typing means that the type of a variable, function parameter, or object property is known at compile time, before the code is executed (before runtime).

Unlike JavaScript, where variables can hold any value at any moment (dynamic typing) and where errors are thrown at runtime when code is executed, TypeScript enforces rules about what types of values can be assigned on the stage of its writing – compilation time. For example:

let age: number = 25;
age = "twenty five"; // ❌ Error: Type 'string' is not assignable to type 'number'

This early detection of errors prevents many common bugs and makes the development process safer.

Benefits of static typing:

  • Early error detection — mistakes are caught during compilation (writing code time) instead of runtime.
  • Improved code readability — types serve as self-documentation.
  • Better tooling support — IDEs can offer autocomplete, refactoring, and hints based on type definitions.
  • Scalability — larger projects become easier to manage and maintain.

Explicit vs. Inferential Typing in TypeScript

TypeScript offers two ways to work with types: explicit typing and type inference.

Explicit Typing

In explicit typing, developers manually specify the type:

let username: string = "Alice"; // the type here is written explicitly - :string
let isAdmin: boolean = true;

function multiply(a: number, b: number): number {
  return a * b;
}

This approach is useful when:

  • The type is not obvious.
  • You want to make the code more descriptive.
  • You want to ensure strict type safety.

Inferential Typing

TypeScript can also infer types automatically, based on assigned values:

let city = "London"; // inferred as string - when the variable is declared and assigned to a certain value it flows from this that here is the type string - this is evident that this is not a number, so type here is inherited from assignment and in future this variable could accept only strings reassignments.
let count = 10;      // inferred as number

In this case, there is no need to explicitly define string or number. The compiler is smart enough to assign them.

Type inference makes code cleaner and reduces redundancy, but developers still retain the ability to add explicit types if needed.

👉 Best practice: Use type inference for simple cases, but prefer explicit typing for function parameters, return types, and complex data structures.


Structured Typing in TypeScript (Duck Typing)

One of the most powerful concepts in TypeScript is structural typing.

Unlike some other languages where type compatibility is based on nominal typing (names of types must match), TypeScript uses a structural type system. This means that compatibility is determined by the shape (structure) of the objects, not their names.

Example:

interface Person {
  name: string;
  age: number;
}

interface User {
  name: string;
  age: number;
}

Interface User and interface Person are fully interchangeable because of their identity. There is no difference which of them you are using in the code.

let user = { name: "Bob", age: 30, city: "Paris" };

function greet(person: Person) {
  console.log(`Hello, ${person.name}`);
}

greet(user); // ✅ Works, because 'user' has at least the required structure

Even though user has an extra property (city), it is still valid because it contains the required structure (name and age).

This principle is sometimes referred to as “duck typing”if it looks like a duck and quacks like a duck, it’s a duck.

Advantages of structured typing:

  • More flexibility when working with objects.
  • Easier integration with third-party libraries.
  • Reduces the need for boilerplate type declarations.

Conclusion

TypeScript’s static typing system brings clarity and reliability to JavaScript development.

  • Static typing ensures type correctness during compilation.
  • Explicit and inferential typing give developers a choice between strictness and convenience.
  • Structured typing allows flexible yet safe handling of objects based on their shape, not just type names.

By understanding and leveraging these features, developers can write cleaner, safer, and more maintainable code, especially in large-scale applications.

Related posts
TypeScript

Literals in TypeScript: String, Number, Boolean, Template, and Compound Literals

Literals are one of the fundamental building blocks of TypeScript programming. They represent fixed…
Read more
TypeScript

Utility Types in TypeScript: Pick, Omit, Partial, Required, Readonly, Record...

✅ — Utility Types are one of the most powerful features of TypeScript, but also one of the most…
Read more
TypeScript

Complete TypeScript Tutorial Online: Master TypeScript in 2025

TypeScript has revolutionized modern web development by bringing static typing to JavaScript, making…
Read more