TypeScript Concepts

Contextual typing — inference from use site

An expected type can supply information to an expression. In a typed callback assignment, an unannotated parameter can receive its type from the function contract. Without a suitable context, an unannotated parameter may trigger noImplicitAny. Contextual typing can also influence literal and object inference. Observed output: '3.0'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.

Formal Syntax & Structure

typescript syntaxtypescript
const callback: (n: number) => string = n => String(n);

Working Example

Example Snippettypescript
const format: (n: number) => string = n => n.toFixed(1);console.log(format(3));
TypeScript Reference