Python-syntax to TypeScript/JavaScript compiler
Project description
TypeSht
A statically-typed programming language that uses Python-like syntax and compiles to TypeScript/JavaScript. Designed for Python developers who want to write type-safe code that runs in JavaScript environments (browsers, Node.js) without learning TypeScript or JavaScript syntax.
If it works in Python, it works the same in TypeSht. No surprises.
Why TypeSht?
Python developers often need to target JavaScript environments — web frontends, Node.js servers, serverless functions. The usual options are:
- Learn JavaScript/TypeScript (steep learning curve)
- Use a transpiler that loses Python semantics (silent bugs)
TypeSht takes a different approach: compile Python-syntax source files to TypeScript, and ship a runtime package (typesht-runtime) that preserves Python behavior faithfully in JavaScript environments. Floor division, negative indexing, Python-style dicts, print() with sep/end — it all works exactly as you'd expect.
Requirements
Compiler:
- Python 3.10+
Runtime & output:
- Node.js 16+
- TypeScript (
npm install -g typescript)
Installation
1. Install the TypeSht compiler (Python):
pip install typesht
2. Install the TypeSht runtime (Node.js):
npm install typesht-runtime
Usage
# Compile to JavaScript (default)
typesht compile myprogram.tsht
# Compile to TypeScript only
typesht compile myprogram.tsht --target ts
# Compile to both JS and TS
typesht compile myprogram.tsht --target both
# Specify output directory
typesht compile myprogram.tsht --output dist/
Output goes to an output/ folder next to your source file by default.
To run the compiled output:
cd output/
npm init -y
# add "type": "module" to package.json
npm install typesht-runtime
node myprogram.js
Language Features
Variables & Type Annotations
name: str = "Alice"
age: int = 30
score: float = 9.5
is_active: bool = True
x = 42 # untyped
F-Strings
msg: str = f"Hello, {name}! You are {age} years old."
result: str = f"Next year: {age + 1}"
Functions
def greet(name: str, age: int) -> str:
return f"Hi, I am {name}"
def add(a: int, b: int) -> int:
return a + b
Lambda Functions
double = lambda x: x * 2
add = lambda x, y: x + y
no_args = lambda: 42
Classes
class Person:
name: str
age: int
def __init__(self, name: str, age: int):
self.name = name
self.age = age
def greet(self) -> str:
return f"Hi, I am {self.name}"
person = Person("Alice", 30)
print(person.greet())
Decorators
class MathHelper:
@staticmethod
def add(x: int, y: int) -> int:
return x + y
@classmethod
def create(cls, base: int) -> int:
return base
@property
def value(self) -> int:
return self._value
# Custom decorators compile to higher-order function calls
@my_decorator
def process(data: str) -> str:
return data
# → process = myDecorator(process)
Generics
numbers: list[int] = [1, 2, 3]
words: list[str] = ["hello", "world"]
data: dict[str, int] = {"a": 1}
def get_first(items: list[int]) -> int:
return items[0]
Lists & Dicts
items: list = [10, 20, 30, 40, 50]
print(items[0]) # → 10
print(items[-1]) # → 50 (negative indexing)
print(items[1:3]) # → [20, 30] (slicing)
data: dict = {"lang": "TypeSht", "version": "0.1"}
Control Flow
if age > 18:
print("Adult")
elif age == 18:
print("Just turned 18")
else:
print("Minor")
for i in range(10):
print(i)
for item in items:
print(item)
for k, v in pairs:
print(f"{k} = {v}")
while x > 0:
x -= 1
Operators
result = 10 // 3 # floor division → 3
remainder = 10 % 3 # modulo → 1
power = 2 ** 8 # power → 256
x = a and b # logical and
y = a or b # logical or
z = not a # logical not
found = item in items # membership test
missing = item not in items
Triple-Quoted Strings
msg = """
This is a
multi-line string
"""
Imports
# Standard library (provided by typesht-runtime)
import math
import random
import json
import re
import sys
import os
# With alias
import random as rng
# From imports
from json import dumps, loads
from math import sqrt, pi
# User modules
import mymodule
from mymodule import my_func
Standard Library
| Module | Available Functions |
|---|---|
math |
pi, e, sqrt, floor, ceil, abs, pow, log, log2, log10, sin, cos, tan, exp, trunc, inf, nan, isnan, isinf, gcd, factorial |
random |
random, randint, uniform, choice, shuffle, sample, seed |
json |
dumps, loads |
re |
match, search, findall, sub, split, compile |
sys |
argv, exit, version, platform |
os |
path.join, path.dirname, path.basename, path.abspath, path.normpath, path.splitext, path.split, path.exists, path.isfile, path.isdir, path.sep, getcwd, getenv |
Type Mappings
| TypeSht | TypeScript |
|---|---|
str |
string |
int |
number |
float |
number |
bool |
boolean |
list |
any[] |
list[int] |
number[] |
list[str] |
string[] |
dict |
any (backed by PyDict runtime class) |
None |
null |
Python Semantics Preserved
TypeSht ships typesht-runtime — an npm package that ensures Python behavior is preserved at runtime:
print()withsepandendkeyword arguments- Negative indexing on strings and lists (
items[-1]) - Slicing on strings and lists (
items[1:3]) - Floor division (
//) toward negative infinity - Python-style modulo (
%) for negative numbers dictwith ordered keys and Python iteration behavior (for k in d,.items(),.values())in/not inmembership testing- Python-style error types (
ValueError,TypeError,IndexError,KeyError,ZeroDivisionError)
Project Structure
typesht/
├── src/typesht/ — pip-installable compiler package
│ ├── lexer.py — Tokenizer
│ ├── parser.py — AST + Parser
│ ├── codegen.py — TypeScript code generator
│ └── main.py — CLI entry point
│
├── compiler/ — Development copy (used by the test runner)
│ ├── lexer.py
│ ├── parser.py
│ ├── codegen.py
│ └── main.py
│
├── runtime/
│ ├── typesht-runtime.ts — Python behavior runtime (source)
│ └── package.json
│
├── tests/
│ └── test_compiler.py — 136 compiler unit tests
│
└── examples/
└── hello.tsht — Comprehensive example program
Running Tests
python -m pytest tests/
Roadmap
- f-string format specs (
f"{value:.2f}") - Exception handling (
try/except/finally) - Multiple inheritance
- Type aliases
- Standard library expansion (
itertools,functools,collections) - Optional JS extras (explicit opt-in JavaScript-specific features)
- Package manager integration (
pip install typesht,npm install typesht-runtime)
Author
Jeiel K Libert
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typesht-0.1.0.tar.gz.
File metadata
- Download URL: typesht-0.1.0.tar.gz
- Upload date:
- Size: 26.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6ed9e292c0d37108524fa41144910acd09a675d3f22dd45deb5dc590f873efe
|
|
| MD5 |
4bc1b7d8966bccfbec9ee6568a9a5c81
|
|
| BLAKE2b-256 |
fa2600ed4387daebeb1999378aef7d6ded00639f894567ac57cead78c44b5200
|
Provenance
The following attestation bundles were made for typesht-0.1.0.tar.gz:
Publisher:
release.yml on jlibert/typesht
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typesht-0.1.0.tar.gz -
Subject digest:
f6ed9e292c0d37108524fa41144910acd09a675d3f22dd45deb5dc590f873efe - Sigstore transparency entry: 1226553354
- Sigstore integration time:
-
Permalink:
jlibert/typesht@d246204c6f267f557900e131b2bfda2c09d4d034 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/jlibert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d246204c6f267f557900e131b2bfda2c09d4d034 -
Trigger Event:
push
-
Statement type:
File details
Details for the file typesht-0.1.0-py3-none-any.whl.
File metadata
- Download URL: typesht-0.1.0-py3-none-any.whl
- Upload date:
- Size: 26.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
018bd8b4b5675d70a1a332cca77d3d6ecdcf6006cb523ffe6278bbdeee6c3f44
|
|
| MD5 |
83e0c74c970cba8e49f4d80f6345d276
|
|
| BLAKE2b-256 |
722bc11b3b7d29eaefa86d00297fee9990ee59a9d0b49c1bb94ed0c96b9a8c3c
|
Provenance
The following attestation bundles were made for typesht-0.1.0-py3-none-any.whl:
Publisher:
release.yml on jlibert/typesht
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typesht-0.1.0-py3-none-any.whl -
Subject digest:
018bd8b4b5675d70a1a332cca77d3d6ecdcf6006cb523ffe6278bbdeee6c3f44 - Sigstore transparency entry: 1226553361
- Sigstore integration time:
-
Permalink:
jlibert/typesht@d246204c6f267f557900e131b2bfda2c09d4d034 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/jlibert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d246204c6f267f557900e131b2bfda2c09d4d034 -
Trigger Event:
push
-
Statement type: