WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Advanced · question 58 of 100

What is ’type compatibility’ in TypeScript and how does it work?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

Type compatibility in TypeScript is a mechanism that determines whether two types can be considered "compatible" or assignable to each other. This feature allows TypeScript to provide powerful type checking while maintaining flexibility in assigning values between different types. Type compatibility in TypeScript is based on structural typing, which means that it considers two types to be compatible if their structures (i.e., properties and methods) match, even if the names of the types are different.

To check if two types are compatible, TypeScript recursively compares the properties of these types, ensuring that the source type’s properties have compatible properties in the target type. If each property in the source type can be found in the target type and their types are compatible, then the whole source type is considered compatible with the target type.

Let’s go through some examples to better understand how type compatibility works in TypeScript:

1. Basic type compatibility:

interface Named {
  name: string;
}

class Person {
  name: string;
}

let p: Named;
// Here, Person and Named are compatible since both have a 'name' property of type string
p = new Person();

2. Function type compatibility:

interface Greet {
  (phrase: string): void;
}

function greet(g: Greet) {
  g("Hello, world!");
}

// Both of the following are compatible with the Greet interface.
const fn1 = (x: string) => console.log(x);
const fn2 = (x: string, y?: number) => console.log(x);

greet(fn1); // OK
greet(fn2); // OK

Function type compatibility is based on the function’s parameter and return types. In the above example, both ’fn1’ and ’fn2’ are compatible with the ’Greet’ interface since their parameter types and return types match.

3. Generic type compatibility:

interface Empty<T> {}

let x: Empty<number>;
let y: Empty<string>;

// Here, Empty<number> and Empty<string> are compatible since their structures (empty) are the same
x = y;

Generic types are compatible if their structures and type arguments are compatible. In the above example, both ’Empty<number>’ and ’Empty<string>’ have an empty structure, so they are considered compatible.

However, it’s important to note that TypeScript’s type compatibility is not always perfect, and there might be cases where it’s too permissive or too strict. In such situations, you can use type assertions or custom type guards to fine-tune the type checking behavior.

Overall, type compatibility in TypeScript is a powerful feature that allows flexible, yet robust type checking and makes it easier to work with different data structures and APIs while maintaining strong safety guarantees.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics