0Pricing
Mojo Academy · Lesson

Type Annotations That Help

Add types so Mojo can optimize and catch errors.

Type Annotations That Help is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Type Annotation?

A type annotation states a value's type right in the declaration, so both you and Mojo know exactly what it holds.

Annotating a var

Add a colon and the type after the name. Here : Int promises that count will only ever hold whole numbers.

var count: Int = 0

Inference vs Annotation

Without an annotation Mojo infers the type from the value. Writing it yourself makes your intent explicit and clear.

var x = 5
var y: Int = 5

Catching Mistakes Early

An annotation lets Mojo reject a wrong value at compile time. Assigning text to an Int becomes an immediate error.

var count: Int = "oops"  # error

Annotations Aid Speed

Knowing the exact type ahead of time, Mojo can generate faster machine code instead of checking types as it runs.

Annotating Floats and Bools

Annotations work for every type. Spell out Float64 or Bool to document precisely what each value is.

var ratio: Float64 = 0.5
var ok: Bool = True

Annotating Strings

Mark text values with String so readers instantly see that a var carries words rather than numbers.

var name: String = "Ada"

Annotations as Documentation

A type next to a name tells the next reader, including future you, what to expect without hunting for the value.

Required in fn Functions

Strict fn functions require type annotations on their parameters, which is where this habit pays off most.

fn add(a: Int, b: Int):
    print(a + b)

Annotate at Boundaries

Even when inference works, annotate the values others rely on. Clear types at boundaries prevent confusion across your code.

Types Are a Safety Net

Annotations turn silent bugs into loud errors and unlock speed. A little typing now saves real debugging later.

Quick Check

You annotate a var as Int but try to assign it the text "oops". What happens in Mojo?

Recap

A type annotation names a value's type with a colon, catching errors early and helping Mojo run faster. 🎯

Frequently asked questions

Is the “Type Annotations That Help” lesson free?

Yes — the full text of “Type Annotations That Help” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type Annotations That Help”?

Add types so Mojo can optimize and catch errors. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type Annotations That Help” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Values with var
  2. Int, Float, and Bool
  3. Strings and Text
  4. Type Annotations That Help
← Back to Mojo Academy