{"id":26522,"library":"ts2php","title":"ts2php","description":"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.","status":"maintenance","version":"0.30.3","language":"javascript","source_language":"en","source_url":"https://github.com/searchfe/ts2php","tags":["javascript","typescript"],"install":[{"cmd":"npm install ts2php","lang":"bash","label":"npm"},{"cmd":"yarn add ts2php","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts2php","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for TypeScript compiler API","package":"typescript","optional":true}],"imports":[{"note":"ts2php ships both ESM and CJS; use ESM for modern projects.","wrong":"const ts2php = require('ts2php')","symbol":"default","correct":"import ts2php from 'ts2php'"},{"note":"Named export 'transpile' is available since v0.28.","wrong":"const { transpile } = require('ts2php');","symbol":"transpile","correct":"import { transpile } from 'ts2php'"},{"note":"CompileOptions is only a type, compile it with import type.","wrong":"import { CompileOptions } from 'ts2php'","symbol":"CompileOptions","correct":"import type { CompileOptions } from 'ts2php'"}],"quickstart":{"code":"import ts2php from 'ts2php';\n\nconst code = `\nfunction add(a: number, b: number): number {\n  return a + b;\n}\n`;\n\nconst result = ts2php(code, {\n  declare: true,\n});\n\nconsole.log(result);\n// Output: PHP source code with declared types if enabled.","lang":"typescript","description":"Transpiles a simple TypeScript function to PHP using ts2php with type declarations enabled."},"warnings":[{"fix":"Install TypeScript 3.7.0 or later: npm install typescript@^3.7.0","message":"Requires TypeScript >= 3.7.0 as peer dependency. Older TypeScript versions are incompatible.","severity":"breaking","affected_versions":"<0.28"},{"fix":"Use ESM imports: import ts2php from 'ts2php';","message":"Do not use require() in Node.js CJS context; use ESM import or call the module with a different syntax.","severity":"gotcha","affected_versions":">=0.30.0"},{"fix":"Use transpile() instead.","message":"The compile() function was renamed to transpile() in v0.28.","severity":"deprecated","affected_versions":"<0.28"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use ESM import: import ts2php from 'ts2php';","cause":"Using require() on an ESM-only export in Node.js CJS context.","error":"TypeError: ts2php is not a function"},{"fix":"Install typescript: npm install typescript@^3.7.0","cause":"Missing TypeScript peer dependency.","error":"Cannot find module 'typescript'"},{"fix":"Use valid modules option: 'commonjs' or 'esm' (not 'amd' or 'system').","cause":"Invalid modules option passed to transpile().","error":"Error: Supplied options.modules is not supported"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}