aeson-generics-typescript: Generates TypeScript definitions that match Generic Aeson encodings

[ bsd3, library, program, web ] [ Propose Tags ]

This project uses GHC.Generics to generate TypeScript type definitions that match the Generic Aseon encoding for the same type. Included here are tests that round trip by compling the TypeScript with tsc, and checking that generated values type check.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0.1
Dependencies aeson (>=2.1.2 && <2.2), base (>=4.17.2 && <4.18), bytestring (>=0.11.5 && <0.12), containers (>=0.6.7 && <0.7), directory (>=1.3.7 && <1.4), filepath (>=1.4.2 && <1.5), hspec (>=2.11.7 && <2.12), process (>=1.6.17 && <1.7), QuickCheck (>=2.14.3 && <2.15), random (>=1.2.1 && <1.3), split (>=0.2.4 && <0.3), string-interpolate (>=0.3.2 && <0.4), text (>=2.0.2 && <2.1), time (>=1.12.2 && <1.13) [details]
License BSD-3-Clause
Copyright 2023
Author Platonic.Systems
Maintainer info@Platonic.Systems
Category Web
Source repo head: git clone https://gitlab.com/platonic/aeson-generics-typescript.git
Uploaded by fresheyeball at 2023-12-13T19:03:37Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Executables tests
Downloads 49 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for aeson-generics-typescript-0.0.0.1

[back to package description]

Aeson Generics TypeScript

This project generates TypeScript type definitions using GHC.Generics, that match Aeson instances generated the same way.

data Foo = ...
  deriving stock (Generic)
  deriving anyclass (ToJSON, FromJSON, TypeScriptDefinition)

Is all it takes to have your TypeScript type definition for your data type. Now you can obtain the TypeScript definition as a string like so.

fooTSDef :: String
fooTSDef = getPrintedDefinition $ Proxy @Foo

Example

You can see many examples in the tests. One provided here for documentation purposes:

data Sum a b = Foyst a
             | Loser b
  deriving stock (Eq, Generic, Ord, Show)
  deriving anyclass (ToJSON, TypeScriptDefinition)

-- getPrintedDefinition must have a concrete type. If you want to retain
-- generic type variables in TypeScript, use `TSGenericVar` to make the
-- type concrete.
sumTSDef :: String
sumTSDef = getPrintedDefinition $ Proxy @(Sum (TSGenericVar "a") (TSGenericVar "b"))

will generate

// Defined in Data.Aeson.Generics.TypeScript.Types of main
type Sum<A,B> = Foyst<A> | Loser<B>;
interface Foyst<A> {
  readonly tag: "Foyst";
  readonly contents: A;
}
interface Loser<B> {
  readonly tag: "Loser";
  readonly contents: B;
}