python2ts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -0
- package/dist/acorn-CDFV7HXL.js +3131 -0
- package/dist/acorn-CDFV7HXL.js.map +1 -0
- package/dist/angular-PCJAXZFD.js +3071 -0
- package/dist/angular-PCJAXZFD.js.map +1 -0
- package/dist/babel-O6RKFHDQ.js +7297 -0
- package/dist/babel-O6RKFHDQ.js.map +1 -0
- package/dist/chunk-DTI6R2GT.js +23942 -0
- package/dist/chunk-DTI6R2GT.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +117 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/estree-467CT4RF.js +4613 -0
- package/dist/estree-467CT4RF.js.map +1 -0
- package/dist/flow-FCFYGKSM.js +27547 -0
- package/dist/flow-FCFYGKSM.js.map +1 -0
- package/dist/glimmer-P46N72G3.js +2895 -0
- package/dist/glimmer-P46N72G3.js.map +1 -0
- package/dist/graphql-TFOZJU7B.js +1267 -0
- package/dist/graphql-TFOZJU7B.js.map +1 -0
- package/dist/html-KZKNLN3R.js +2934 -0
- package/dist/html-KZKNLN3R.js.map +1 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown-BS2B5AKD.js +3554 -0
- package/dist/markdown-BS2B5AKD.js.map +1 -0
- package/dist/meriyah-W6HUPGCY.js +2685 -0
- package/dist/meriyah-W6HUPGCY.js.map +1 -0
- package/dist/postcss-LMJBCD2Q.js +5081 -0
- package/dist/postcss-LMJBCD2Q.js.map +1 -0
- package/dist/typescript-Y5EPKFX5.js +13204 -0
- package/dist/typescript-Y5EPKFX5.js.map +1 -0
- package/dist/yaml-ZNP3H3BX.js +4225 -0
- package/dist/yaml-ZNP3H3BX.js.map +1 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# python2ts
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/python2ts)
|
|
4
|
+
[](https://github.com/sebastian-software/python2ts/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
**AST-based Python to TypeScript transpiler** — Write Python, ship TypeScript.
|
|
7
|
+
|
|
8
|
+
Converts Python code to clean, idiomatic TypeScript with full type preservation.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install python2ts
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## CLI Usage
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Transpile a file
|
|
20
|
+
npx python2ts input.py -o output.ts
|
|
21
|
+
|
|
22
|
+
# Pipe from stdin
|
|
23
|
+
cat script.py | npx python2ts > script.ts
|
|
24
|
+
|
|
25
|
+
# Preview without runtime import
|
|
26
|
+
npx python2ts input.py --no-runtime
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API Usage
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { transpile } from "python2ts"
|
|
33
|
+
|
|
34
|
+
const typescript = transpile(`
|
|
35
|
+
def fibonacci(n: int) -> list[int]:
|
|
36
|
+
a, b = 0, 1
|
|
37
|
+
result = []
|
|
38
|
+
for _ in range(n):
|
|
39
|
+
result.append(a)
|
|
40
|
+
a, b = b, a + b
|
|
41
|
+
return result
|
|
42
|
+
`)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Output:**
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { range } from "pythonlib"
|
|
49
|
+
|
|
50
|
+
function fibonacci(n: number): number[] {
|
|
51
|
+
let [a, b] = [0, 1]
|
|
52
|
+
let result: number[] = []
|
|
53
|
+
for (const _ of range(n)) {
|
|
54
|
+
result.push(a)
|
|
55
|
+
;[a, b] = [b, a + b]
|
|
56
|
+
}
|
|
57
|
+
return result
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## What Gets Transpiled?
|
|
62
|
+
|
|
63
|
+
| Python | TypeScript |
|
|
64
|
+
| ---------------------------- | ------------------------------------- |
|
|
65
|
+
| `int`, `str`, `bool`, `None` | `number`, `string`, `boolean`, `null` |
|
|
66
|
+
| `list[T]`, `dict[K,V]` | `T[]`, `Record<K,V>` |
|
|
67
|
+
| `Optional[T]` | `T \| null` |
|
|
68
|
+
| `def fn():` | `function fn()` |
|
|
69
|
+
| `lambda x: x + 1` | `(x) => x + 1` |
|
|
70
|
+
| `class Child(Parent):` | `class Child extends Parent` |
|
|
71
|
+
| `@dataclass` | Auto-generated constructor |
|
|
72
|
+
| `for x in items:` | `for (const x of items)` |
|
|
73
|
+
| `if/elif/else` | `if/else if/else` |
|
|
74
|
+
| `match/case` | `switch/case` |
|
|
75
|
+
| `async def` / `await` | `async function` / `await` |
|
|
76
|
+
| `//` (floor div) | `floordiv()` (Python semantics) |
|
|
77
|
+
| `%` (modulo) | `mod()` (Python semantics) |
|
|
78
|
+
| `arr[1:-1]` (slicing) | `slice()` (full support) |
|
|
79
|
+
| `f"Hello {name}"` | `` `Hello ${name}` `` |
|
|
80
|
+
| `"""docstring"""` | `/** JSDoc */` |
|
|
81
|
+
|
|
82
|
+
## Advanced API
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { parse, transform, generate } from "python2ts"
|
|
86
|
+
|
|
87
|
+
// Step-by-step transformation
|
|
88
|
+
const ast = parse(pythonCode)
|
|
89
|
+
const transformed = transform(ast)
|
|
90
|
+
const { code, usedRuntimeFunctions } = generate(transformed, {
|
|
91
|
+
includeRuntime: true,
|
|
92
|
+
runtimeImportPath: "pythonlib"
|
|
93
|
+
})
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Runtime Library
|
|
97
|
+
|
|
98
|
+
python2ts uses [pythonlib](https://www.npmjs.com/package/pythonlib) for Python standard library
|
|
99
|
+
functions:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Generated imports (grouped by module)
|
|
103
|
+
import { range, enumerate, len } from "pythonlib"
|
|
104
|
+
import { chain, combinations } from "pythonlib/itertools"
|
|
105
|
+
import { Counter } from "pythonlib/collections"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
pythonlib is automatically included as a dependency.
|
|
109
|
+
|
|
110
|
+
## Type Hints Preserved
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from typing import Optional, Callable, TypeVar
|
|
114
|
+
|
|
115
|
+
T = TypeVar('T')
|
|
116
|
+
|
|
117
|
+
def process(
|
|
118
|
+
items: list[T],
|
|
119
|
+
callback: Callable[[T, int], bool],
|
|
120
|
+
default: Optional[str] = None
|
|
121
|
+
) -> dict[str, T]:
|
|
122
|
+
...
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Becomes:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
function process<T>(
|
|
129
|
+
items: T[],
|
|
130
|
+
callback: (arg0: T, arg1: number) => boolean,
|
|
131
|
+
default_: string | null = null
|
|
132
|
+
): Record<string, T> {
|
|
133
|
+
// ...
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Requirements
|
|
138
|
+
|
|
139
|
+
- Node.js >= 22.0.0
|
|
140
|
+
|
|
141
|
+
## Related
|
|
142
|
+
|
|
143
|
+
- [pythonlib](https://www.npmjs.com/package/pythonlib) — Python standard library for TypeScript
|
|
144
|
+
(standalone runtime)
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT
|