Dhall-lang: Generate Typescript Types from (some) Dhall Types

Created on 5 Feb 2020  路  4Comments  路  Source: dhall-lang/dhall-lang

I'd like to add a command-line tool that generates Typescript types for a subset of Dhall types. For example, given the following Dhall type declaration (https://github.com/dhall-lang/dhall-haskell/issues/1383#issuecomment-538670891):

let Foo: Type = < Left : Natural | Right : Bool> in Foo

This command (e.g. dhall ts-delcaration) would generate the typescript type:

type Foo =
| {
   tag: 'Left',
   value: number
}
| {
   tag: 'Right'
   value: boolean
}

The idea being that JSON generated from a Dhall value with the Dhall type above would have the Typescript type above. This command would presumably need to take the same arguments as dhall-to-json (e.g. --nesting-nested=value --tag-field=name), and the same parameters would have to be passed during the generation of the JSON values and the generation of the Typescript declarations.

If this had been implemented, adding support for generation of "codecs" (https://gcanti.github.io/io-ts/) would not be very difficult.

This would make Dhall much more useful in the Typescript ecosystem, and would potentially be useful even for users who are not interested (initially) in storing their configuration in Dhall format. They could simply use Dhall type declarations to generate codec code.

Does this strike anyone as a worthwhile project? What subset of Dhall types could realistically be supported? What would be a sensible default mapping between JSON-serializable Typescript types and Dhall types?

@sjakobi: Is there a consensus around adding the "tag-field" and "nesting" command-line options?

Most helpful comment

All 4 comments

@bwestergard: I think this would be worthwhile!

I think the main Dhall types / type-level functions to support are:

  • Bool
  • Text
  • Natural
  • Integer
  • Double
  • List
  • Optional
  • Records
  • Unions
  • Possibly Prelude.JSON.Type

For everything except the last type I think there is a sensible mapping to Typescript types

I have a WIP branch for https://github.com/dhall-lang/dhall-haskell/issues/1383. I had stopped working on it when I realized that I had a bug in the handling of expressions that had been manually annotated with Prelude.JSON.Nesting IIRC. I should try to finish that work but I can't promise when I'll get to it. :/

Regarding the CLI options, I went with:

Usage: dhall-to-json <...>
                     ([--union-nesting-inline] | [--union-nesting-nested FIELD])
                     [--union-tag-field FIELD] <...>

In the case of --union-nesting-inline there is the little complication that we don't have a field name for any non-empty non-record alternative contents. We decided to error out if we encounter any union types where that is the case.

@sjakobi Cool. No rush, I probably won't have time to work on this for a few months anyway.

RE: --union-nesting-inline. For the purposes of this generator, I just wouldn't types which include non-empty non-record variants.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nadrieril picture Nadrieril  路  3Comments

bch29 picture bch29  路  4Comments

sjakobi picture sjakobi  路  5Comments

asivitz picture asivitz  路  6Comments

hanshoglund picture hanshoglund  路  4Comments