Giter Site home page Giter Site logo

pixura / purescript-ohyes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from justinwoo/purescript-ohyes

0.0 4.0 0.0 14 KB

A library for generating Typescript types that can be used transparently from Purescript.

License: MIT License

PureScript 90.82% TypeScript 9.18%

purescript-ohyes's Introduction

Purescript-OhYes

Build Status

A library for generating Typescript types that can be used transparently from Purescript.

See the blog post here: https://github.com/justinwoo/my-blog-posts#you-can-interop-with-typescript-using-purescript-ohyes

This is likely not the "silver bullet" that you are looking for, but can give you some good ideas on how to get going.

This library also provides ways for working with typical union type forms of records with a discriminant field by using Variant, which uses a record representation with a string literal type field and the associated value field.

export type VariantTest =
  | { type: "a", value: string }
  | { type: "b", value: number }
  | { type: "c", value: boolean };

Example

The tests generate types and write them to a file, which is then checked with Typescript using test/test.ts:

type A =
  { a :: Number
  , b :: String
  , c :: { d :: String }
  , e :: Array String
  , f :: Nullable String
  , g :: Number -> Number -> Number
  , h :: Fn2 Number Number Number
  , i :: Fn2 Number (Fn2 Number Number Number) Number
  }

type VariantTest = Variant
  ( a :: String
  , b :: Number
  , c :: Boolean
  )

generateTSFile :: _
generateTSFile = writeTextFile UTF8 "./test/generated.ts" values
  where
    values = format defaultOptions $ intercalate "\n"
      [ generateTS "A" (Proxy :: Proxy A)
      , generateTS "VariantTest" (Proxy :: Proxy VariantTest)
      ]

Generated types:

export type A = {
  a: number,
  b: string,
  c: { d: string },
  e: string[],
  f: string | null,
  g: (a: number) => (a: number) => number,
  h: (a: number, b: number) => number,
  i: (a: number, b: (a: number, b: number) => number) => number
};
export type VariantTest =
  | { type: "a", value: string }
  | { type: "b", value: number }
  | { type: "c", value: boolean };

See the additional example here: https://github.com/justinwoo/ohyes-demo

purescript-ohyes's People

Contributors

charlescrain avatar justinwoo avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.