How to Test if Two Types are Exactly the Same in TypeScript ?
Last Updated :
28 Apr, 2025
This article will show how we can test if the two types are the same in TypeScript. Types in Typescript are used to define and describe the data in our code. Some of the types are number, string, boolean, etc.
Below are the possible approaches:
Using extends and keyof
In this approach, we are using extends and keyof to check if the keys of type T extend the key of type U and also if the key of type U extends the key of type T. When both of the conditions are true then we can display true as a result which indicated that both types are the exactly same set of keys.
Syntax:
type TypeEquality<T, U> = keyof T extends keyof U ? (keyof U extends keyof T ? true : false) : false;
Example: Below is the implementation of the above-discussed approach.
JavaScript
type approach1<T, U> = keyof T extends keyof U ?
(keyof U extends keyof T ? true : false) : false;
type Type1 = { a: number; b: string };
type Type2 = { a: number; b: string };
const res1: approach1<Type1, Type2> = true;
// Types are same
console.log(res1);
const res2: approach1<Type1, {
a: number;
b: string; c: boolean
}> = false; // Types are different
console.log(res2);
Output:
true
false
Using Extract and keyof
In this approach, we are using the Extract and keyof to check if there are no common keys between the two types of T and U. If there are no common keys then it is true and we are printing true results which are the same types.
Syntax:
type TypeEquality<T, U> = Extract<keyof T, keyof U> extends never ? false : true;
Example: Below is the implementation of the above-discussed approach.
JavaScript
type approach2<T, U> = Extract<keyof T, keyof U> extends never
? Extract<keyof U, keyof T> extends never
? true
: false
: false;
type Type1 = { a: number; b: string };
type Type2 = { a: number; b: string };
const res1: approach2<Type1, Type2> =
true as approach2<Type1, Type2>;
// Types are the same
console.log(res1);
const res2: approach2<Type1, {
a: number;
b: string; c: boolean
}> = false as approach2<Type1,
{ a: number; b: string; c: boolean }>;
// Types are different
console.log(res2);
Output:
true
false
Using Mapped Types
In this approach, we are using Mapped Types in which we are checking if each key in type T is the same key in type U. If the condition is true, then the two types are the same. Using this approach, we can make sure that there is proper one-to-one mapping of keys within the two types.
Syntax:
type TypeEquality<T, U> = { [K in keyof T]: K extends keyof U ? T[K] : never } extends T ? true : false;
Example: Below is the implementation of the above-discussed approach.
JavaScript
type approach3<T, U> = keyof T extends keyof U
? keyof U extends keyof T
? true
: false
: false;
type Type1 = { a: number; b: string };
type Type2 = { a: number; b: string };
const res1: approach3<Type1, Type2> = true;
// Types are same
console.log(res1);
const res2: approach3<Type1, {
a: number;
b: string; c: boolean
}> = false;
// Types are different
console.log(res2);
Output:
true
false
Similar Reads
How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th
3 min read
How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in
2 min read
How To Get Types From Arrays in TypeScript? In TypeScript, arrays are a common data structure, but sometimes it's necessary to extract the types of elements stored in an array for type-checking or validation purposes. TypeScript provides several ways to extract types from arrays, enabling more type-safe operations.We will explore different me
3 min read
How to Check if an Object is Empty in TypeScript ? In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null. Below are the methods to check if an object is empty or not in TypeScript: Table o
3 min read
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read