02open 25 min

Syntax and structure mapping: functions, objects, modules, and annotations

Map Python functions, dictionaries, modules, and annotations onto TypeScript's function signatures, object types, and module surfaces.

by the end of this lesson you can

  • Uses TypeScript parameter and return annotations clearly
  • Introduces an object type only if it improves readability
  • Keeps module and naming style aligned with idiomatic TypeScript

Overview

Python developers already know how code organization shapes readability. In TypeScript, the translation work is not only syntax. You need to decide what belongs in a function signature, what should become an object type, and how module exports communicate a stable interface.

In Python, you often

use functions, dictionaries, classes, and module-level names with type hints that guide readers without fully controlling runtime shape.

In TypeScript, the common pattern is

to express the contract directly in function signatures, object property types, and exported module surfaces so the compiler can help preserve intent.

why this difference matters

This lesson helps Python developers stop translating line-by-line and start designing TypeScript code in the places where the language is strongest: signatures, object modeling, and explicit module contracts.

Python

def normalize(name: str, enabled: bool = True) -> str:
    if not enabled:
        return name
    return name.strip().lower()

TypeScript

export function normalize(name: string, enabled = true): string {
  if (!enabled) {
    return name;
  }

  return name.trim().toLowerCase();
}

Deeper comparison

Python version

def load_config() -> dict[str, str]:
    return {"mode": "dev", "region": "us"}

TypeScript version

type Config = {
  mode: string;
  region: string;
};

function loadConfig(): Config {
  return { mode: "dev", region: "us" };
}

Reflect

Where does TypeScript gain the most from explicit signatures and exported module contracts compared with lightly annotated Python code?

what a strong answer notices

A strong answer mentions clearer caller expectations, editor support, safer refactors, and using type positions to define interfaces more deliberately.

Rewrite

Rewrite this Python helper into TypeScript with a function signature and object shape that feel native to the TypeScript ecosystem.

Rewrite this Python

def build_user(name: str, role: str) -> dict[str, str]:
    return {"name": name, "role": role}

what good looks like

  • Uses TypeScript parameter and return annotations clearly
  • Introduces an object type only if it improves readability
  • Keeps module and naming style aligned with idiomatic TypeScript

Practice

Design a small TypeScript module that exports a typed loadProfile helper and a typed Profile shape for its result.

success criteria

  • Makes the exported contract obvious
  • Uses object typing intentionally rather than as a Python dict substitute
  • Keeps the module surface small and easy to scan

Common mistakes

  • Porting Python dict-return helpers without deciding whether a named TypeScript type would clarify the contract.
  • Adding annotations mechanically while leaving the module interface vague.
  • Assuming function type syntax is just decoration instead of part of the API design.

takeaways

  • This lesson helps Python developers stop translating line-by-line and start designing TypeScript code in the places where the language is strongest: signatures, object modeling, and explicit module contracts.
  • A strong answer mentions clearer caller expectations, editor support, safer refactors, and using type positions to define interfaces more deliberately.
  • Makes the exported contract obvious