0.1.11 • Published 1 year ago

@greenlabs/ppx-ts v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

PPX_ts

ppx_ts is a ReScript preprocessor, which helps generating helper types to bind to typescript modules.

Install

yarn add -D @greenlabs/ppx-ts
// bsconfig.json
"ppx-flags": [
  ...,
  "@greenlabs/ppx-ts/ppx"
],

Features

Attributes @

keyOf

@ppx_ts.keyOf
type t = {
  name: string,
  age: int
}

// automatically generated
type t_keyOf = Name | Age

// automatically generated
let t_keyToString = key =>
  switch key {
  | Name => "name"
  | Age => "age"
  }

setType(t)

module Error = {
  type t
}
@ppx_ts.setType(Error.t)
type t = {
  name: string,
  age: int
}

// automatically generated
type t_setType = {
  name: Error.t
  age: Error.t
}

toGeneric

@ppx_ts.toGeneric
type t = {
  name: string,
  age: int
}

// automatically generated
type t_toGeneric<'a> = {
  name: 'a
  age: 'a
}

partial

@ppx_ts.partial
type t = {
  name: string,
  age: int
}

// automatically generated
type t_partial = {
  name: option<string>,
  age: option<int>
}

pick

@ppx_ts.pick(["name"])
type t = {
  name: string,
  age: int
}

// automatically generated
type t_pick_name = {
  name: string,
}

omit

@ppx_ts.omit(["name"])
type t = {
  name: string,
  age: int
}

// automatically generated
type t_omit_name = {
  age: int,
}

toArray

@ppx_ts.toArray
type t = Name | Age

// automatically generated
let t_toArray = ["Name", "Age"]

Extension %

keyOf

module Error = {
  type t = {
    name: string,
    age: int
  }
}

@spice // attributes are available
type t1 = %ppx_ts.keyOf(Error.t) // type t1 = Name | Age

// automatically generated
let t1_keyToString = key =>
  switch key {
  | Name => "name"
  | Age => "age"
  }

setType

type t = {
  name: string,
  age: int
}

@spice // attributes are available
type t1 = %ppx_ts.setType((t, string))
// changed
type t1 = {
  name: string
  age: string
}

setTypeExceptBool

type t = {
  name: string,
  age: int,
  isKorean: bool
}

@spice // attributes are available
type t1 = %ppx_ts.setTypeExceptBool((t, string))
// changed
type t1 = {
  name: string,
  age: string,
  isKorean: bool
}

toGeneric

type t = {
  name: string,
  age: int
}

@spice // attributes are available
type t1 = %ppx_ts.toGeneric(t)
// generated
type t1<'a> = {
  name: 'a
  age: 'a
}

partial

type t = {
  name: string,
  age: int,
  isKorean: bool
}

@spice // attributes are available
type t1 = %ppx_ts.partial(t)
// generated
type t1 = {
  name: option<string>,
  age: option<int>,
  isKorean: option<bool>
}

Contribution

  1. Create a sandbox with opam
opam switch create ppx_ts 4.12.1
  1. Install dependencies
opam install . --deps-only --with-test
  1. Build
opam exec -- dune build
  1. Test
cd rescript

(install dependencies)
yarn

(build --watch)
yarn res:clean && yarn res:watch

(run test --watch)
yarn test:watch
0.1.10

1 year ago

0.1.11

1 year ago

0.1.7-rc1

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago