TypeScript
Type Inference
TypeScript automatically infers types.
By EZ4Code Team
inferenceinference
Code
let x = 10; // number
let s = "hello"; // string
let arr = [1, 2]; // number[]
let obj = { a: 1, b: "x" }; // { a: number; b: string }
// Function return type inference
function add(a: number, b: number) {
return a + b; // Infers return number
}
// Destructure inference
const { a, b } = obj; // a: number, b: string
// const assertion as literal
const config = { mode: "dev" } as const; // { readonly mode: "dev" }Explanation
TS automatically infers types based on context, reducing explicit annotations.