GO
TS

Go to TypeScript

10 lessons

Progress0%
1Variables & Types2Functions3Structs & Classes4Interfaces — Both Use Structural Typing5Error Handling6Goroutines vs Async/Await7Slices & Maps → Arrays & Objects8Packages & Modules9Testing10Generics
All Mirror Courses
GO
TS
Variables & Types
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
GO
From Go:

In Go, you're familiar with declaring variables and understanding the type systems.

TS
In TypeScript:

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:

TS
TypeScript 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:

GO
Go (What you know)
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, 20
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go has separate int/float64 types; TypeScript has just number

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go uses := for short declaration; TypeScript uses let/const

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go is compiled; TypeScript transpiles to JavaScript

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

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.

GO
Go
var age int = 30
var score float64 = 9.5
TS
TypeScript
let age: number = 30;
let score: number = 9.5;
Rule of Thumb
Go's int/float64 both become number in TypeScript.

2. Variable Declaration

Go's := short-form works only inside functions; TypeScript uses let (reassignable) or const (block-scoped constant).

GO
Go
city := "Istanbul"
TS
TypeScript
let city = "Istanbul";   // reassignable
const city = "Istanbul"; // immutable
Rule of Thumb
Prefer const in TypeScript; use let only when you need reassignment.

3. Type Inference

Both languages infer types from initializers, so explicit annotations are optional but recommended for public APIs.

Common Pitfall
TypeScript's any type disables type checking — avoid it like Go's interface{}.

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
Common Pitfall
Don't assume TypeScript works exactly like Go. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Go code in TypeScript to practice these concepts.
OverviewNext