TypeScript experiment

This commit is contained in:
Umur Ozkul 2022-08-17 18:57:34 +02:00
parent 3f92e7c3e1
commit a8c7297621
15 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,33 @@
open ForTS_Types
@module("ForTS_InternalValue_tag") @scope("InternalValueTag")
external ivtVoid_: int = "IvtVoid"
@module("ForTS_InternalValue_tag") @scope("InternalValueTag")
external ivtString_: int = "IvtString"
@module("ForTS_InternalValue_tag") @scope("InternalValueTag")
external ivtRecordLike_: int = "IvtRecordLike"
let getTag = (variant: internalValue) =>
switch variant {
| IvVoid(_) => ivtVoid_
| IvString(_) => ivtString_
| IvRecordLike(_) => ivtRecordLike_
}
let getVoid = (variant: internalValue): option<internalVoid> =>
switch variant {
| IvVoid(v) => Some(v)
| _ => None
}
let getString = (variant: internalValue): option<string> =>
switch variant {
| IvString(s) => Some(s)
| _ => None
}
let getRecordLike = (variant: internalValue): option<recordLike> =>
switch variant {
| IvRecordLike(r) => Some(r)
| _ => None
}

View File

@ -0,0 +1,8 @@
open ForTS_Types
@genType
let toString = (v: recordLike): string =>
MyInterface_InternalValue_RecordLike.toString(v, MyInterface_InternalValue.toString)
@genType
let toArray = (v: recordLike): array<(string, internalValue)> =>
MyInterface_InternalValue_RecordLike.toArray(v)

View File

@ -0,0 +1 @@
@genType let toString = (value: string): string => value

View File

@ -0,0 +1,3 @@
open ForTS_Types
@genType let toString = (_value: internalVoid): string => MyInterface_InternalValue_Void.toString

View File

@ -0,0 +1,7 @@
enum InternalValueTag {
IvtVoid,
IvtString,
IvtRecordLike
}

View File

@ -0,0 +1,4 @@
open ForTS_Types
@genType
let getResult = (_p: myProject): option<result_internalValue> => My_ErrorValue.EError->Error->Some

View File

@ -0,0 +1,18 @@
open ForTS_Types
@genType let isError = (r: result_internalValue): bool => Belt.Result.isError(r)
@genType let isOk = (r: result_internalValue): bool => Belt.Result.isOk(r)
@genType
let getError = (r: result_internalValue): option<errorValue> =>
switch r {
| Ok(_) => None
| Error(e) => Some(e)
}
@genType
let getValue = (r: result_internalValue): option<internalValue> =>
switch r {
| Ok(v) => Some(v)
| Error(_) => None
}

View File

@ -0,0 +1,12 @@
/*
Group all opaque types together for TS.
All other modules for TS should use this module
*/
@genType.opaque type internalValue = MyInterface_InternalValue_T.t
@genType.opaque type recordLike = MyInterface_InternalValue_RecordLike.t
@genType.opaque type internalVoid = int
@genType.opaque type errorValue = My_ErrorValue.t
@genType.opaque type result_internalValue = result<internalValue, errorValue> // There has to be a type for each result permutation
@genType.opaque type myProject = {name: string}
//There is no need to map option<> as it becomes nullable

View File

@ -0,0 +1,10 @@
type errorValue = EError | EErrorString(string) | EErrorNumber(float)
type t = errorValue
let toString = (e: errorValue): string => {
switch e {
| EError => "Error"
| EErrorString(s) => s
| EErrorNumber(f) => Js.Float.toString(f)
}
}

View File

@ -0,0 +1,9 @@
open MyInterface_InternalValue_T
let rec toString = (v: internalValue): string => {
switch v {
| IvString(s) => MyInterface_InternalValue_String.toString(s)
| IvRecordLike(m) => MyInterface_InternalValue_RecordLike.toString(m, toString)
| IvVoid(_v) => MyInterface_InternalValue_Void.toString
}
}

View File

@ -0,0 +1,12 @@
type t = MyInterface_InternalValue_T.recordLike
let toString = (value: t, recToString) => {
let contents =
Belt.Map.String.mapWithKey(value, (key, value) => {
`${key}: ${recToString(value)}`
})
->Belt.Map.String.toArray
->Js.Array2.joinWith(", ")
`{${contents}}`
}
let toArray = (value: t) => Belt.Map.String.toArray(value)

View File

@ -0,0 +1,7 @@
type rec internalValue =
| IvString(string)
| IvRecordLike(recordLike)
| IvVoid(int)
and recordLike = Belt.Map.String.t<internalValue>
type t = internalValue

View File

@ -0,0 +1 @@
To be trashed. Experimental code