A lightweight and typesafe code generation library built for interfacing with your Node projects using Swift.
enum TypeSwift {
case toggle
case setLabel(_ text: String)
case addNumbers(_ a: Double, _ b: Double)
case selectDevice(_ device: Device)
enum Device {
case Phone, Pad, Mac
}
// ...
}
Which can then be used in your Swift project as such (no additional setup required):
webView.ts(.toggle)
webView.ts(.setLabel("Hello, world!"))
webView.ts(.addNumbers(9, 5))
webView.ts(.selectDevice(.Phone))
Creating a native SwiftUI Toolbar item that replaces the functionality of a React NavBar (46 seconds).
Walthrough.mp4
Refer to the TypeSwift Wiki for full, in-depth documentation.
There are two main components to this project: SwiftGen and WKExtensions.
A node package that incorporates ts-morph to fulfill the following purpose:
- Uses ts-morph to extract variables, functions, data structures and types from your code
- Maps each TS type to a Swift equivalent for compatible calling
- Generates a Swift data structure for easy interfacing
- Stores the compiled JavScript calling method for evaluation in a WebKit object
Identifier extraction techniques are detailed here.
This Swift package allows you to interface with the TypeScript code, from within your Swift code, using a familiar syntax style. The enum data structure allows for certain features, such as auto-completion, in your favorite Swift IDE.
An optional custom WebKit object that will allow you to interface with your Node project directly. No setup required. Just plugin and play!
Currently in development. See ObservableWebView for more details on the base foundation.
Note: I'm very new to publishing with NPM! If you have any suggestions for how to improve this flow, please open an issue.
-
Drag the
TypeSwift/
folder into your Xcode project:- Make sure "Copy items if needed" is checked
- Select "Create groups"
-
Navigate to
SwiftGen/
-
Run
npm install
-
Modify the
config.json
:
{
"inputDir": "path/to/ts-files",
"outputDir": "path/to/xcode-proj/TypeSwift",
"outputFileName": "TypeSwift",
"outputSuffix": ".swift",
}
inputDir
: Path to directory containing the TypeScript files. Will search recursively. Resolves both relative and absolute paths.outputDir
: Absolute path to theTypeSwift/
folder that you dragged into your Xcode project.
- Build in the CLI:
swiftgen
You're ready to start interacting with TypeScript in your Swift code! If you add new TypeScript variables or functions, simply build again with swiftgen
to generate an updated file.
Refer to Generation Schema by Example for more details.
let toggle = TypeSwift.toggle
webView.ts(toggle)
// or
webView.ts(.toggle)
webView.somePromise() { result, error
print(result)
}
let result = await webView.anotherPromise()
Preview.mp4
Still working on the docs. Check back soon!