Variables & Types
Declaring variables and understanding the type systems
Introduction
In this lesson, you'll learn about variables & types in TypeScript. Coming from Go, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Go, you're familiar with declaring variables and understanding the type systems.
TypeScript has its own approach to declaring variables and understanding the type systems, which we'll explore step by step.
The TypeScript Way
Let's see how TypeScript handles this concept. Here's a typical example:
let name: string = "Alice";
let age: number = 30;
let score: number = 9.5;
let active: boolean = true;
// Type inference
let city = "Istanbul"; // inferred as string
// Constants
const MAX_RETRIES = 3;
// Destructuring assignment
let [x, y] = [10, 20];Comparing to Go
Here's how you might have written similar code in Go:
package main
var name string = "Alice"
var age int = 30
var score float64 = 9.5
var active bool = true
// Short declaration
city := "Istanbul"
// Constants
const MaxRetries = 3
// Multiple assignment
x, y := 10, 20You may be used to different syntax or behavior.
Go has separate int/float64 types; TypeScript has just number
You may be used to different syntax or behavior.
Go uses := for short declaration; TypeScript uses let/const
You may be used to different syntax or behavior.
Go is compiled; TypeScript transpiles to JavaScript
You may be used to different syntax or behavior.
TypeScript runs in browsers; Go compiles to native binaries
Step-by-Step Breakdown
1. Primitive Types
Go has distinct integer and float types; TypeScript simplifies to a single number type covering both integers and floating point.
var age int = 30
var score float64 = 9.5let age: number = 30;
let score: number = 9.5;2. Variable Declaration
Go's := short-form works only inside functions; TypeScript uses let (reassignable) or const (block-scoped constant).
city := "Istanbul"let city = "Istanbul"; // reassignable
const city = "Istanbul"; // immutable3. Type Inference
Both languages infer types from initializers, so explicit annotations are optional but recommended for public APIs.
Common Mistakes
When coming from Go, developers often make these mistakes:
- Go has separate int/float64 types; TypeScript has just number
- Go uses := for short declaration; TypeScript uses let/const
- Go is compiled; TypeScript transpiles to JavaScript
Key Takeaways
- Go's int/float64 → TypeScript's number
- Go's := → TypeScript's let/const
- Both support type inference
- TypeScript compiles to JS; Go compiles to native