TypeScript Basics: Return Type Inference
Welcome to the Return Type Inference lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Functions' return types can be inferred, which saves us from typing them out. As with all inference, the types still exist and are still enforced. Inference only means that the compiler can determine the types for us.
>
function two() {return 1 + 1;}two();Result:
2
>
function two() {return 1 + 1;}let s: string = two();s;Result:
type error: Type 'number' is not assignable to type 'string'.