Skip to content

hackages/you-dont-know-typescript-part-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeScript Typing Exercises

Welcome! This repository contains a series of TypeScript exercises designed to help you practice and improve your type system skills.

Overview

The goal of these exercises is to add proper type annotations to functions that are currently working but not well-typed. All tests are already passing, so your job is to improve the type safety of the codebase by replacing implicit any types with proper TypeScript types.

Exercise Order

Complete the exercises in the following order:

  1. addNumbers - Start with basic number typing
  2. addStrings - Practice string-specific typing
  3. attempt - Handle function and error typing
  4. constant - Work with generic types
  5. identity - Master generics and optional parameters
  6. noop - Type functions with arbitrary arguments
  7. times - Combine generics with function callbacks

Your Task

Each function in the src/ directory has tests that are passing, but the functions lack proper type annotations. Your job is to:

  • Add appropriate type annotations to function parameters
  • Add return type annotations where helpful
  • Use generics (<T>) when appropriate
  • Replace any implicit any types with specific types
  • Ensure type safety while maintaining the existing functionality

The "Any" Penalty System

⚠️ Important: Each any type found in your code costs 5 euros, and you have a 20 euro credit to use during this session.

This means you can have at most 4 instances of any in your codebase before you exceed your budget. Use your credit wisely!

Tips to Avoid any:

  • Use specific types (string, number, boolean, etc.)
  • Use generics (<T>, <U>, etc.) for functions that work with multiple types
  • Use union types (string | number) when a value can be one of several types
  • Use unknown instead of any when the type is truly unknown (though you'll still need to narrow it)
  • Use function types for callbacks and higher-order functions

Getting Started

  1. Run the tests to see that they're passing:

    npm test
  2. Start with the first exercise (addNumbers) and work through them in order

  3. After adding types, run the tests again to ensure everything still works:

    npm test
  4. Check for any any types in your code - remember, each one costs 5 euros!

Good Luck!

Take your time, think about the types carefully, and remember: the goal is type safety, not just making the tests pass (they already do!). Happy typing! 🎯

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published