swift-js

raw JSON →
0.1.0 verified Fri May 01 auth: no javascript abandoned

A Swift-to-JavaScript transpiler (v0.1.0) that converts a subset of Swift syntax into JavaScript. Provides basic support for classes, functions, variables, and control flow. Not actively maintained; experimental status. Differentiators vs alternative transpilers (e.g., Kotlin/JS, Scala.js) include minimal dependencies and a simple CLI workflow, but lacks robust error handling, Unicode support, and standard library mapping. Suitable for proof-of-concept projects only.

error Cannot find module 'swift-js'
cause Package not installed or not in a valid npm registry.
fix
Install via 'npm install swift-js' from npm registry.
error 'require' is not defined in ES module scope
cause Using CommonJS require() with an ESM-only package.
fix
Use 'import { transpile } from 'swift-js';' or set 'type': 'module' in package.json.
error Unexpected token: '('
cause Swift syntax not supported by the transpiler (e.g., closures with shorthand arguments).
fix
Rewrite unsupported Swift constructs using basic function definitions.
gotcha Swift string interpolation escaped incorrectly in JavaScript output.
fix Manually replace \(...) with template literal syntax ${...} after transpilation.
gotcha Does not support Swift's Optional types; nil may be mapped to undefined incorrectly.
fix Handle undefined values manually in generated JS code.
deprecated Package has not been updated since 2020; consider using modern alternatives like Kotlin/JS or TypeScript.
fix Migrate to a supported transpiler.
npm install swift-js
yarn add swift-js
pnpm add swift-js

Basic example transpiling a Swift function to JavaScript, including error handling for transpilation failures.

import { transpile } from 'swift-js';

const swiftCode = `
func greet(name: String) -> String {
    return "Hello, \(name)!"
}
print(greet(name: "World"))
`;

try {
  const jsCode = transpile(swiftCode);
  console.log(jsCode);
} catch (err) {
  console.error('Transpilation error:', err.message);
}