Converter
Python to TypeScript converter
Paste Python and get TypeScript. Thunder reads the Python into Thunder Core, then compiles that to TypeScript, with the same compiler as the thunder command. The TypeScript gives the same results as the Python. Lines Thunder can't convert with the same meaning are named, never guessed.
✓ 3 of 3 functions converted exactly.
Type or paste your own. It converts as you type, in your browser: nothing is sent anywhere.
In TypeScript
// Generated by the Thunder compiler from main.thunder.
// A build artifact: change main.thunder and build again, or keep this file and edit it as TypeScript.
// It needs only Node.js and thunder_runtime.mjs (written next to it). "// thunder:N" marks the Thunder line.
import * as T from './thunder_runtime.mjs';
const FILE = "main.thunder";
export function shipping_cost(weight: any, express: any): any { // thunder:1
const F = T.enterCall(FILE, 1, 3);
try {
let cost: any;
F.line = 3; // thunder:3
if ((F.line = 3, T.compare("<=", weight, 0n))) { // thunder:3
F.line = 4; throw T.Stop(T.pyStr("weight must be positive")); // thunder:4
}
F.line = 5; cost = T.add(5n, T.mul(weight, 2n)); // thunder:5
F.line = 6; // thunder:6
if ((F.line = 6, (T.yesNo(express) === true))) { // thunder:6
F.line = 7; cost = T.mul(cost, T.dec("1.5")); // thunder:7
}
F.line = 8; return cost; // thunder:8
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export function grade(score: any): any { // thunder:10
const F = T.enterCall(FILE, 10, 12);
try {
F.line = 12; // thunder:12
if ((F.line = 12, T.compare(">=", score, 90n))) { // thunder:12
F.line = 13; return "A"; // thunder:13
} else if ((F.line = 14, T.compare(">=", score, 80n))) { // thunder:14
F.line = 15; return "B"; // thunder:15
} else if ((F.line = 16, T.compare(">=", score, 70n))) { // thunder:16
F.line = 17; return "C"; // thunder:17
}
F.line = 18; return "F"; // thunder:18
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export function total(prices: any): any { // thunder:20
const F = T.enterCall(FILE, 20, 22);
try {
let result: any, price: any;
F.line = 22; result = 0n; // thunder:22
F.line = 23; for (const $el1 of T.pyIter(prices)) { // thunder:23
F.line = 23; price = $el1; // thunder:23
F.line = 24; result = T.add(result, price); // thunder:24
}
F.line = 25; return result; // thunder:25
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export const THUNDER_TESTS: [string, () => void, number][] = [
];What you get
- TypeScript that runs and gives the same results as the Python: exact decimals, the same errors, the same output. The same test programs check every target.
- Generated code, not hand-written style. Values are dynamic, and the code uses a small Thunder runtime file that
thunder buildwrites next to it. It runs, and you can keep and edit it, but it won't read like TypeScript a person would write. Python type hints don't change the output yet. - Each line ends with a mark showing the Thunder line it came from, so you can trace any line back.
What converts today
- Functions with numbers, text, yes/no values, and lists;
if/elif/else;forloops; arithmetic;raise; and classes (each method becomes a function that receives the object). - Measured on 20 small Python functions: 19 convert exactly, and all 19 give the same results as the original Python on every sample input. See the measurements.
- Not yet:
try/except, and special methods such as__len__. These are marked not translated, with the reason.
The example, step by step
Written by the Thunder compiler 2.0.0 while this page was built, not by hand.
Python
def shipping_cost(weight, express):
"""Five dollars plus two per kilo; express costs half as much again."""
if weight <= 0:
raise ValueError("weight must be positive")
cost = 5 + weight * 2
if express:
cost = cost * 1.5
return cost
def grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
return "F"
def total(prices):
result = 0
for price in prices:
result = result + price
return resultThunder Core (the middle step)
To shipping cost:
Receive weight and express
When weight is at most 0:
Stop with "weight must be positive"
Set cost to 5 + (weight * 2)
When express is true:
Set cost to cost * 1.5
Give cost
To grade:
Receive score
When score is at least 90:
Give "A"
Otherwise when score is at least 80:
Give "B"
Otherwise when score is at least 70:
Give "C"
Give "F"
To total:
Receive prices
Set result to 0
For each price in prices:
Set result to result + price
Give resultTypeScript
// Generated by the Thunder compiler from main.thunder.
// A build artifact: change main.thunder and build again, or keep this file and edit it as TypeScript.
// It needs only Node.js and thunder_runtime.mjs (written next to it). "// thunder:N" marks the Thunder line.
import * as T from './thunder_runtime.mjs';
const FILE = "main.thunder";
export function shipping_cost(weight: any, express: any): any { // thunder:1
const F = T.enterCall(FILE, 1, 3);
try {
let cost: any;
F.line = 3; // thunder:3
if ((F.line = 3, T.compare("<=", weight, 0n))) { // thunder:3
F.line = 4; throw T.Stop(T.pyStr("weight must be positive")); // thunder:4
}
F.line = 5; cost = T.add(5n, T.mul(weight, 2n)); // thunder:5
F.line = 6; // thunder:6
if ((F.line = 6, (T.yesNo(express) === true))) { // thunder:6
F.line = 7; cost = T.mul(cost, T.dec("1.5")); // thunder:7
}
F.line = 8; return cost; // thunder:8
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export function grade(score: any): any { // thunder:10
const F = T.enterCall(FILE, 10, 12);
try {
F.line = 12; // thunder:12
if ((F.line = 12, T.compare(">=", score, 90n))) { // thunder:12
F.line = 13; return "A"; // thunder:13
} else if ((F.line = 14, T.compare(">=", score, 80n))) { // thunder:14
F.line = 15; return "B"; // thunder:15
} else if ((F.line = 16, T.compare(">=", score, 70n))) { // thunder:16
F.line = 17; return "C"; // thunder:17
}
F.line = 18; return "F"; // thunder:18
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export function total(prices: any): any { // thunder:20
const F = T.enterCall(FILE, 20, 22);
try {
let result: any, price: any;
F.line = 22; result = 0n; // thunder:22
F.line = 23; for (const $el1 of T.pyIter(prices)) { // thunder:23
F.line = 23; price = $el1; // thunder:23
F.line = 24; result = T.add(result, price); // thunder:24
}
F.line = 25; return result; // thunder:25
return null;
} catch (e) { throw T.fail(e); } finally { T.leave(); }
}
export const THUNDER_TESTS: [string, () => void, number][] = [
];On your computer
The same conversion with the thunder command (Node.js 18+). Nothing is sent anywhere, and no AI is involved.
npm install -g @skillstech/thunderlang
thunder read shipping.py --as-core > shipping.thunder
thunder build shipping.thunder --core --target typescript # writes generated/typescript/