Registry / serialization / ts2php

ts2php

JSON →
library0.30.3jsnpmunverified

ts2php is a TypeScript to PHP transpiler (v0.30.3, latest release Jan 2021) that converts TypeScript/JavaScript code into PHP. It aims to enable code sharing between front-end and back-end by translating TypeScript into PHP. Key differentiators: it supports a large subset of TypeScript features including classes, interfaces, generics, async/await, and standard library polyfills. It is read-only (not a bidirectional bridge) and generates PHP code that can be used in any PHP project. Releases have slowed, indicating maintenance mode. Requires TypeScript ^3.7.0 as a peer dependency.

npm install ts2php
INSTALL
IMPORT
SIG · TS2PHP
T
ts2php
serializationjavascriptv0.30.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import ts2php from 'ts2php'
const ts2php = require('ts2php')
ts2php ships both ESM and CJS; use ESM for modern projects.
transpile
import { transpile } from 'ts2php'
const { transpile } = require('ts2php');
Named export 'transpile' is available since v0.28.
CompileOptions
import type { CompileOptions } from 'ts2php'
import { CompileOptions } from 'ts2php'
CompileOptions is only a type, compile it with import type.

Transpiles a simple TypeScript function to PHP using ts2php with type declarations enabled.

import ts2php from 'ts2php'; const code = ` function add(a: number, b: number): number { return a + b; } `; const result = ts2php(code, { declare: true, }); console.log(result); // Output: PHP source code with declared types if enabled.
ts2php --version
Debug
Known issues
breakingRequires TypeScript >= 3.7.0 as peer dependency. Older TypeScript versions are incompatible.
fix
Install TypeScript 3.7.0 or later: npm install typescript@^3.7.0
affects: <0.28
gotchaDo not use require() in Node.js CJS context; use ESM import or call the module with a different syntax.
fix
Use ESM imports: import ts2php from 'ts2php';
affects: >=0.30.0
deprecatedThe compile() function was renamed to transpile() in v0.28.
fix
Use transpile() instead.
affects: <0.28
Errors
Common errors & fixes
TypeError: ts2php is not a function
Using require() on an ESM-only export in Node.js CJS context.
fix
Use ESM import: import ts2php from 'ts2php';
Cannot find module 'typescript'
Missing TypeScript peer dependency.
fix
Install typescript: npm install typescript@^3.7.0
Error: Supplied options.modules is not supported
Invalid modules option passed to transpile().
fix
Use valid modules option: 'commonjs' or 'esm' (not 'amd' or 'system').
Upgrade
Version history
0.30.3latest on npm
Audit
Dependencies
typescriptoptionalPeer dependency for TypeScript compiler API
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources