CodingNeed.

1 · Write contracts · 8 MIN

Type a function

Functions turn inputs into outputs.

Parameter and return annotations make a function contract explicit. TypeScript still runs as JavaScript after compilation. Runtime tests and compile-time checking serve different purposes.

A small machine takes an ingredient, performs one operation, and gives you a result.

Read the example

function double(n: number): number { return n * 2; }
console.log(double(4));
Check the expected output
8

Your challenge

Return the larger of input.a and input.b. Equal values should return that value.

Common trap

Logging an answer does not return it to a caller.

Next lesson: Describe an object →

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.