@hackage aeson-generics-typescript0.0.0.1
Generates TypeScript definitions that match Generic Aeson encodings
Categories
License
BSD-3-Clause
Maintainer
info@Platonic.Systems
Links
Versions
- 0.0.0.1 Wed, 13 Dec 2023
Installation
Dependencies (5)
Dependents (1)
@hackage/servant-aeson-generics-typescript
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;
}