TypeScript Inference Last Updated : 21 Jan, 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 Example, TypeScript 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.More Examples of TypeScript Inference Inference of Variable 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 Type 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 Comment More infoAdvertise with us Next Article TypeScript Inference P pranjalisingh1201 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Literal Inference Type TypeScript Literal Inference Type allows you to create type annotations based on actual values, variables, or constants. This enhances type safety and code clarity by specifying that a variable can only hold specific, exact values. It's a way to express the precise values that a variable can take on 2 min read TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As 2 min read TypeScript Assertion functions TypeScript Assertion functions are nothing but the functions that allow us to mainly create the custom type guards that assert the type of a value on our specific criteria. The most suitable use of Assertion Functions is when we are working with the more complex data structures. Syntax:function asse 3 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read Statistical Inference Statistical inference is the process of using data analysis to infer properties of an underlying distribution of a population. It is a branch of statistics that deals with making inferences about a population based on data from a sample. Statistical inference is based on probability theory and proba 8 min read Like