Scala to TypeScript code generator
Scala
Switch branches/tags
Clone or download
miloszpp Merge pull request #15 from returntocorp/prepend-I-prefix
Config option to prefix interfaces with "I" prefix
Latest commit aa4db8c Jun 17, 2018
Permalink
Failed to load latest commit information.
project upgrade to sbt 1.0 Oct 29, 2017
src add config key support for prepend I Jun 4, 2018
.gitignore Initial commit Jun 12, 2016
LICENCE.txt Create LICENCE.txt Jun 13, 2016
README.md fix in readme Nov 17, 2017
build.sbt Cleanup and fixed unit test Nov 17, 2017

README.md

scala-ts

Overview

scala-ts is a simple tool which can generate TypeScript interfaces and classes from Scala case classes.

scala-ts is helpful when working with REST-ful Scala backend and TypeScript frontend. Having defined Scala types returned by your endpoints you can easily generate TypeScript definitions for consuming these endpoints.

http://codewithstyle.info/scala-ts-scala-typescript-code-generator/

New version 0.3.2 - added support for more types; added file output support.

New version 0.4.0 - added support for SBT 1.0, Either and Map.

Usage

scala-ts can be used either standalone or as a sbt plugin.

Standalone

Run com.mpc.scalats.Main class and provide a space separated list of fully qualified class names which you want to generate TypeScript for.

Example:

java -cp 'scala-ts-assembly-0.1.0.jar' com.mpc.scalats.Main "com.example.ExampleDto"

SBT plugin

Add the following plugin to plugins.sbt:

addSbtPlugin("com.github.miloszpp" % "scala-ts" % "0.3.0")

Additionally, enable the plugin in your project settings:

enablePlugins(com.mpc.scalats.sbt.TypeScriptGeneratorPlugin)

Now you can use the generateTypeScript command in SBT. For example:

sbt "generateTypeScript com.example.ExampleDto"

Configuration

Starting from release 0.3.0, it's possible to specify some configuration options:

  • emitInterfaces - generate interface declarations (true by default)
  • emitClasses - generate class declarations (false by default)
  • optionToNullable - translate Option types to union type with null (e.g. Option[Int] to number | null)
  • optionToUndefined - translate Option types to union type with undefined (e.g. Option[Int] to number | undefined) - can be combined with optionToNullable
  • outputStream - the stream to which the code should be emitted; it defaults to console

Usage example in build.sbt:

emitClasses in generateTypeScript := true

enablePlugins(com.mpc.scalats.sbt.TypeScriptGeneratorPlugin)

Type support

Currently scala-ts supports the following types of case class members:

  • Int, Double, Boolean, String, Long
  • List, Seq, Set
  • Option
  • LocalDate, Instant, Timestamp
  • generic types
  • References to other case classes

Development

Quickstart

sbt
runMain com.mpc.scalats.AuthorExample

Credits

Many thanks to: