TypeScript Inference Last Updated : 09 Sep, 2025 Comments Improve Suggest changes Like Article Like Report TypeScript's type inference automatically determines the types of variables, function return values, objects, and arrays based on their assigned values and usage.This feature reduces the need for explicit type annotations, simplifying code while maintaining type safety.By analyzing the context and initial values, TypeScript ensures that variables and functions operate with consistent and expected types throughout the codebase. JavaScript let age = 25; let name = "John"; console.log(`Age: ${age}`); console.log(`Name: ${name}`); In this ExampleTypeScript infers age as a number and name as a string based on their assigned values.This automatic detection ensures type safety without requiring explicit annotations.Inference of Variable TypeVariable type inference means the programming language automatically deduces the type of a variable from the value assigned to it, without the programmer explicitly specifying the type. JavaScript let x = 10; // TypeScript infers x as a number console.log(typeof x); In this Example, TypeScript infers the type of x as number based on the initial value 10.This ensures x can only hold numerical values, improving type safety.Output:numberInference of Array Type JavaScript let fruits = ["Apple", "Banana", "Cherry"]; // TypeScript infers fruits as string[] console.log(fruits); In this Example,TypeScript infers the type of fruits as an array of strings (string[]) based on the initial values.This prevents adding elements of other types, maintaining array consistency.Output:[ 'Apple', 'Banana', 'Cherry' ]Inference of Function Return TypeArray type inference means the compiler or interpreter automatically determines the type of an array based on the elements it contains. Instead of explicitly declaring the array’s type, the language infers it from the assigned values. JavaScript function add(a: number, b: number) { return a + b; // TypeScript infers the return type as number } console.log(add(5, 10)); In this Example,The add function's return type is inferred as number because it returns the sum of two numbers.This ensures the function always returns a numerical value, avoiding type-related errors.Output:15 Create Quiz Comment P pranjalisingh1201 Follow 0 Improve P pranjalisingh1201 Follow 0 Improve Article Tags : TypeScript Geeks Premier League 2023 Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like