Day 2: Your First Lines of TypeScript Code

Day 2: Your First Lines of TypeScript Code

Hey there,

Welcome back to Day 2 of the Daily TypeScript challenge! Yesterday, we answered the big "why" and got our tools ready. A huge congrats to everyone who shared their setup—it's awesome to see the community coming together.

Today, the theory stops. We're going to write some actual code.

Thinking in Types

The big shift from JavaScript to TypeScript is one simple idea: we tell our code what kind of data to expect ahead of time. Think of it like putting labels on your moving boxes. Instead of a generic "Box," you have "Kitchen Stuff," "Books," and "Important Documents." It makes everything easier to manage and far less likely that you'll open the wrong one.

In TypeScript, we start with three main "labels" or basic types:

  • string: For text (e.g., "Hello, world")
  • number: For all numbers (e.g., 42, 3.14)
  • boolean: For true or false values

Here’s how you use them. It’s just one extra little piece of syntax.

// We use a colon ":" after the variable name to declare its type.

let myName: string = "Alex";
let currentYear: number = 2025;
let isLearning: boolean = true;        

That’s it! Now, if you try to assign the wrong type of data, TypeScript will immediately warn you.

But is TypeScript Smart? (Yes, it is)

You might be thinking, "Do I have to label everything?"

No, you don't. TypeScript is smart. If you declare and assign a variable on the same line, it can usually figure out the type on its own. This is called Type Inference.

let myCity = "Tirunelveli"; // TypeScript knows this is a string, no need for : string
let myAge = 25; // TypeScript knows this is a number.        

Even without writing the type, your editor will still protect you from mistakes if you try to assign the wrong value later.

The Escape Hatch: any

What if you genuinely dont know what type a variable will be? TypeScript gives you an escape hatch: the any type.

let aWildVariable: any = "This could be a string...";
aWildVariable = 100; // ...or it could be a number. TypeScript won't complain.        

Warning: Using any is like taking the labels off your boxes. You lose all the safety and autocomplete benefits of TypeScript for that variable. Use it very sparingly, usually when you're first migrating old JavaScript code.

Your Day 2 Challenge: Get Your Hands Dirty

It’s time to compile your first .ts file. This will show you how your TypeScript code turns into the plain JavaScript that browsers can run.

  1. Create a file: In your code editor, create a new file and name it day2.ts.
  2. Write the code: Copy and paste the first code block from this email (with the myName, currentYear, and isLearning variables) into your new file.
  3. Compile it: Open your terminal, navigate to the folder where you saved your file, and run this command:

tsc day2.ts        

  1. See the result: You will see a new file appear in your folder: day2.js. Open it! You'll see that it's the same JavaScript code, just without the type annotations.

You've officially written and compiled TypeScript!


That's a huge step. You've now experienced the core workflow.

Tomorrow, we'll level up and see how TypeScript handles the data structures you use every single day: arrays and objects.

See you then,

Manoj Prabhakar


To view or add a comment, sign in

More articles by Manoj Prabhakar

  • Build Agentic Apps using Tyepscript

    A Practical, Modern Guide Agentic apps aren’t “coming.” They’re already here — and the teams who learn to build them…

  • The "Brain" of Your TypeScript Project

    Hey there, So far, we've been working with single files, compiling them one at a time like this: . This is perfect for…

  • When Order Matters - An Intro to Tuples

    Hey, welcome back. Yesterday, we learned about union types, which let us say a variable can be a OR a .

  • When One Type Isn't Enough

    Hey, welcome back. So far, we've put our data into neat, single-type boxes.

  • A Quick Recap & What's Coming Next

    Hey there, Let's take a moment to look back at the foundational skills we've covered so far. You've built a solid base…

  • Let's Build Something!

    Hey, Guys , You've put in the work learning the core concepts all week. Now it's time for the fun part: putting it all…

  • The Pro Way to Define Your Data

    Hey, Happy Friday! You've made it through a full week of daily learning, and that's awesome. We've covered some serious…

  • The TypeScript Feature You'll Use Every Day

    Hey there, and welcome back to Day 4. So far, we’ve organized our data.

  • TypeScript for Your Lists and Data

    Hey, welcome back! Yesterday we learned how to label our individual variables (, , ). That’s a great start.

  • Is TypeScript Really Worth the Hype? Let's Find Out.

    Hey there, And welcome to Day 1 of our 30-day journey into TypeScript. I’m really glad you’re here.

    2 Comments

Explore content categories