Convert code between languages
A port that preserves the awkward edge-case behaviour instead of the happy path — which is exactly where naive translations break.
When to use it
Porting a function to another language where the two languages disagree on something subtle — truthiness, integer division, null handling, string encoding.
What it prevents
The port that works on the happy path and breaks on the edge. Models translate the obvious behaviour faithfully and miss the language-specific quirk — empty-list truthiness, off-by-one in a range. Naming the quirk and demanding a note on anything not preservable is what surfaces the gap.
The prompt
Convert the function below from Python to idiomatic TypeScript.
### Context
The target codebase uses strict TypeScript and prefers pure functions. The Python version relies on the truthiness of an empty list, which does not translate directly.
### Requirements
- Output format: A single TypeScript code block, then two sentences on any behaviour that could not be preserved exactly.
- Keep the same function name and argument order.
- Handle the empty-input case the way the Python version does, not the way TypeScript truthiness would.
- Do not add a runtime dependency; use the standard library only.
- Avoid: the any type and non-null assertions.
### Function
"""
def total(items):
if not items:
return 0
return sum(i.price for i in items)
"""Instant checks
States a concrete task
PassedAvoids vague language
PassedSpecifies an output format
PassedGives context or audience
PassedAvoids dangling references
PassedAvoids over-aggressive phrasing
PassedDefines what a good answer looks like
Passed
Why each part is there
Every section of that prompt exists because a specific failure happens without it. These are the checks it passes, and what each one is protecting you from:
- States a concrete task — why this matters
- Avoids vague language — why this matters
- Specifies an output format — why this matters
- Gives context or audience — why this matters
- Avoids dangling references — why this matters
- Avoids over-aggressive phrasing — why this matters
- Defines what a good answer looks like — why this matters
How to adapt this one
Swap the function and call out the specific semantic mismatch between your two languages — that is the part the model is most likely to get subtly wrong. Keep the instruction to report what could not be preserved exactly; a port with no caveats is either trivial or hiding a difference. Keep the signature and argument order fixed so callers do not have to change.
The mistake this prompt avoids
Assuming a translation is behaviour-preserving because it compiles. Languages differ in exactly the places that do not raise an error — a falsy empty collection, a rounding rule, a default encoding. The compiler catches syntax, not semantics. Ask the model to name the behavioural differences, because those are the bugs that ship silently.
A variation
Porting more than a single function — a module, a small library? Ask for the public interface first, with the idioms mapped (this language's iterator becomes that language's generator), before any line-by-line translation. A whole-module port done function-by-function produces code that runs but reads like the source language wearing a costume; mapping idioms first makes it native.
Check your version
Once you have adapted it, run it through the checker. The same nine checks that graded this page will grade yours, free and in your browser — paste it in, or build one from scratch by answering a few questions.