Python to TypeScript Converter Online
Paste a Python function or script and get a TypeScript starting point. Type annotations, f-strings, control flow, and common builtins are translated; anything ambiguous is flagged for you to finish by hand.
How it works
Python and TypeScript look similar enough that a mechanical translation gets you most of the way, but they are different enough that the output needs review. This tool is a translator, not a compiler: it does not parse Python properly and will not tell you whether your program is correct.
Type annotations map to their TypeScript equivalents. Builtins such as str, int, float, and bool become string, number, number, and boolean. Optional[T] becomes T | null, Union[A, B] becomes A | B, List[T] becomes T[], and Dict[K, V] becomes Record<K, V>. Annotations it does not recognise become unknown rather than being guessed at.
f-strings become template literals, with {expression} interpolated. Slices inside f-strings, such as {items[1:3]}, cannot be expressed directly in JavaScript and are emitted as-is so you can fix them — the tool flags them rather than silently producing a different result.
Control flow maps directly: if becomes if, elif becomes else if, else becomes else, and while and for become their JavaScript equivalents. Because Python is indentation-based and JavaScript is brace-delimited, an else clause emits a closing brace and a new else block. Any block left unterminated at the end of the input is closed automatically and reported.
Things this tool cannot translate are marked with a TODO comment and a warning rather than guessed at. Python imports and decorators have no direct equivalent, so they are skipped with a note. Anything dynamic — metaclasses, monkey-patching, generators, the GIL, runtime attribute injection — needs a human.
Read the output before using it. The value here is in getting the structure and the types right so you are editing rather than writing from scratch.
Use the Python to TypeScript Converter
Paste python on the left to see the result.Runs entirely in your browser — nothing you paste is uploaded or logged.
Worked examples
Real input and the exact output this page produces.
Example 1
Python
def greet(name: str) -> str:
return f"Hello, {name}!"
TypeScript
function greet(name: string): string {
return `Hello, ${name}!`;
}
Example 2
Python
x: int = 5
print(x)
TypeScript
const x: number = 5;
console.log(x);
Frequently asked questions
Will the output always compile?
Not guaranteed. This is a translator, not a compiler. Constructs with no direct equivalent are replaced by TODO comments and reported as warnings so you can finish them.
How are type annotations handled?
Common annotations map to their TypeScript equivalents, including Optional to a union with null, Union to a pipe, List to an array, and Dict to Record. Unknown annotations become unknown.
What happens to Python imports?
They are skipped with a warning. JavaScript uses import statements with different semantics, so mapping Python module names automatically would usually be wrong.
Does it handle async code?
async def becomes async function, and await is preserved. Async generators and Python-specific concurrency idioms are not translated.
Related tools
Others doing similar work.