Copyright | (c) 2018 Tom McLaughlin |
---|---|
License | BSD3 |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
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 $ getTypeScriptDeclarations (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)
- deriveTypeScript :: Options -> Name -> Q [Dec]
- class TypeScript a where
- formatTSDeclarations :: [TSDeclaration] -> String
- formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String
- data FormattingOptions = FormattingOptions {}
- class HasJSONOptions a where
Documentation
:: Options | Encoding options. |
-> Name | Name of the type for which to generate a |
-> Q [Dec] |
Generates a TypeScript
instance declaration for the given data type or data family instance constructor.
The main typeclass
class TypeScript a where Source #
The typeclass that defines how a type is turned into TypeScript.
getTypeScriptDeclarations :: Proxy a -> [TSDeclaration] Source #
Get the declaration(s) needed for this type.
getTypeScriptType :: Proxy a -> String Source #
Get the type as a string.
getTypeScriptOptional :: Proxy a -> Bool Source #
Get a flag representing whether this type is optional.
Formatting declarations
formatTSDeclarations :: [TSDeclaration] -> String Source #
Same as formatTSDeclarations'
, but uses default formatting options.
formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String Source #
Format a single TypeScript declaration. This version accepts a FormattingOptions object in case you want more control over the output.
data FormattingOptions Source #
FormattingOptions | |
|
Convenience tools
class HasJSONOptions a where Source #
Convenience typeclass class you can use to "attach" a set of Aeson encoding options to a type.
getJSONOptions :: Proxy a -> Options Source #