Skip to main content
This guide covers the core TypeScript syntax you’ll use when writing infrastructure and application code: variable declarations, primitive types, explicit typing, and type inference. We’ll follow a practical example — Elmer Codde building a small TypeScript app to manage the ducks on his pond — to demonstrate the concepts in context. Project layout (example in the lab)
The lab environment already has TypeScript installed and configured, and the project is ready to run. Example TypeScript definitions for the duck app (enums, union type, interface, and a simple class that implements the interface):
If you want to follow along locally in the lab environment:
A simple runtime example:
When the dev process is running, changes to files restart compilation and show runtime logs automatically. Meet Elmer Codde: he’s building an app to manage ducks on his pond. The app will include common actions such as making ducks quack, fly, and land. The first step is storing basic information about each duck (name, age, type, color).
A slide titled "Elmer – Introduction" shows a developer icon labeled "Elmer Code" with a speech bubble "Build an app." An arrow labeled "Manage" points to a blue "Pond" containing nine circular duck icons, each labeled "duck."

Variables in TypeScript

TypeScript builds on JavaScript’s const, let, and var keywords but adds static typing. Best practice is to prefer const and let over var. Examples Explicit typing with const (cannot be reassigned):
Attempting to reassign a const triggers a compile-time error:
Using let allows reassignment:

Explicit typing vs. type inference

TypeScript supports both explicit annotations and inference. When the type is obvious from the initializer, you can omit the annotation and let the compiler infer the type.
  • Explicit typing: you declare variable types directly.
  • Type inference: TypeScript infers the variable’s type from its initial value and enforces it afterward.
Examples:
Tip: Use explicit types for public APIs, exported values, and complex structures. For local variables with obvious initial values, type inference keeps code concise while preserving type safety.

Quick checklist: variables & primitives

  • Prefer const for values that won’t be reassigned.
  • Use let for mutable bindings.
  • Avoid var in new code.
  • Favor type inference for local, simple values; add explicit types for public interfaces and complex values.
  • The TypeScript compiler enforces types at build time, reducing runtime type errors.
That concludes this focused overview of variables and primitives in TypeScript. Continue to the next section to learn about Arrays & Objects and how to model collections of ducks.
A horizontal seven-step timeline for a programming course with numbered circular nodes; step 01 "Variables & Parameters" is highlighted. The other nodes read Arrays & Objects, Types & Interfaces, Functions, Classes, Union Types & Enums, and Putting it All Together (© KodeKloud).

Watch Video