Overview of TypeScript variable declarations, primitive types, explicit typing, and type inference demonstrated via a simple duck pond example
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)
EXPLORERCODE.vscode.prettierrc.profile.yarnrc.ymlindex.tspackage.jsontsconfig.jsonyarn.lockroot in ~/code via ⬢ v20.17.0 on ⬢ (us-east-1)❯
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):
// Enum for Duck Typesenum DuckType { Mallard = "Mallard", Muscovy = "Muscovy", Pekin = "Pekin",}// Type for Duck Colors using a union typetype DuckColor = "White" | "Brown" | "Black" | "Mixed";// Interface for a Duck's propertiesinterface IDuck { name: string; // Name of the duck age: number; // Age of the duck in years type: DuckType; // Type of duck, using the DuckType enum color: DuckColor; // Color of the duck, using the DuckColor union type favoriteToy?: string; // Optional property: favorite toy of the duck}// Simple PondDuck class that implements the IDuck interfaceclass PondDuck implements IDuck { constructor( public name: string, public age: number, public type: DuckType, public color: DuckColor, public favoriteToy?: string ) {} quack(): void { console.log(`${this.name} says "quack"!`); } fly(): void { console.log(`${this.name} takes off!`); } land(): void { console.log(`${this.name} lands on the pond.`); }}
If you want to follow along locally in the lab environment:
# Install dependencies (run once when you open the project)yarn install# Start the development build/watch processyarn dev
A simple runtime example:
console.log("Let's go!!");
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).
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:
// Explicit typinglet duckType: string = "Mallard";// Type inference (inferred as string)let duckColor = "Black";// The following assignment would cause a type error because duckColor is inferred as a string:// duckColor = 1; // Error: Type 'number' is not assignable to type 'string'.
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.
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.