aeson-typescript: Generate TypeScript definition files from your ADTs

[ bsd3, json, library, text, web ] [ Propose Tags ]

Modules

[Index]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.1.0, 0.1.2.0, 0.1.3.0, 0.2.0.0, 0.3.0.0, 0.3.0.1, 0.4.0.0, 0.4.1.0, 0.4.2.0, 0.5.0.0, 0.6.0.0, 0.6.1.0, 0.6.2.0, 0.6.3.0
Change log ChangeLog.md
Dependencies aeson, base (>=4.7 && <5), containers, interpolate, mtl, template-haskell, text, th-abstraction (<0.3), unordered-containers [details]
License BSD-3-Clause
Copyright 2017 CodeDown
Author Tom McLaughlin
Maintainer tom@codedown.io
Revised Revision 1 made by phadej at 2019-05-03T17:58:33Z
Category Text, Web, JSON
Home page https://github.com/codedownio/aeson-typescript#readme
Bug tracker https://github.com/codedownio/aeson-typescript/issues
Source repo head: git clone https://github.com/codedownio/aeson-typescript
Uploaded by thomasjm at 2018-01-28T02:20:36Z
Distributions LTSHaskell:0.6.3.0, NixOS:0.6.3.0, Stackage:0.6.3.0
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 8615 total (79 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-01-28 [all 1 reports]

Readme for aeson-typescript-0.1.0.0

[back to package description]

Welcome to aeson-typescript Hackage Build Status

This library provides a way to generate TypeScript .d.ts files that match your existing Aeson ToJSON instances. If you already use Aeson's Template Haskell support to derive your instances, then deriving TypeScript is as simple as

$(deriveTypeScript myAesonOptions ''MyType)

For example,

data D a = Nullary
         | Unary Int
         | Product String Char a
         | Record { testOne   :: Double
                  , testTwo   :: Bool
                  , testThree :: D a
                  } deriving Eq

Next we derive the necessary instances.

$(deriveTypeScript (defaultOptions {fieldLabelModifier = drop 4, constructorTagModifier = map toLower}) ''D)

Now we can use the newly created instances.

>>> putStrLn $ formatTSDeclarations $ getTypeScriptDeclaration (Proxy :: Proxy D)

type D<T> = "nullary" | IUnary<T> | IProduct<T> | IRecord<T>;

type IUnary<T> = number;

type IProduct<T> = [string, string, T];

interface IRecord<T> {
  tag: "record";
  One: number;
  Two: boolean;
  Three: D<T>;
}

It's important to make sure your JSON and TypeScript are being derived with the same options. For this reason, we include the convenience 'HasJSONOptions' typeclass, which lets you write the options only once, like this:

instance HasJSONOptions MyType where getJSONOptions _ = (defaultOptions {fieldLabelModifier = drop 4})

$(deriveJSON (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)
$(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)