Skip to content

Commit f71093b

Browse files
authored
Update typescript.md
1 parent 921b710 commit f71093b

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

docs/typescript.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# LambdaBuffers to Typescript
2-
This chapter will walk through a translation from a LambdaBuffers' module into
3-
a Typescript module which includes the translation of types.
2+
This chapter will walk through a translation from a LambdaBuffers' module into a Typescript module.
43

54
To demonstrate this, we will use the `lbf-prelude-to-typescript` CLI tool which is just a convenient wrapper over the raw `lbf` CLI.
6-
To this end, we may enter a development shell which provides many other Lambda Buffers CLI tools as follows.
5+
To this end, we may enter a development shell which provides this tool along with many other Lambda Buffers CLI tools with the following command.
76

87
```shell
98
$ nix develop github:mlabs-haskell/lambda-buffers#lb
@@ -71,7 +70,7 @@ autogen/build.json
7170
The generated `autogen` directory created contains the generated TypeScript modules.
7271

7372
Note that `lbf-list-modules-typescript` is needed to create a JSON object which maps package names (for NPM) to Lambda Buffers' modules.
74-
Thus, in this example one should have a `package.json` file which associates the key `"name"` with the string value `"lbf-document"`.
73+
Thus, in this example, one should have a `package.json` file which associates the key `"name"` with the string value `"lbf-document"`.
7574

7675
The `autogen/build.json` file can be ignored.
7776

@@ -149,7 +148,7 @@ where the `...` denotes iterated `SomeTypei` for some `i`, then
149148
## Sum types
150149
The types `Author`, `Reviewer`, and `RichContent` have been declared as sum types in the LambdaBuffers schema using the `sum` keyword.
151150
152-
In general, sum types are mapped to a [union type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) in TypeScript and follows the additional following rules.
151+
In general, sum types are mapped to a [union type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) in TypeScript and with the additional following rules.
153152
Given a LambdaBuffers' sum type as follows
154153
155154
```purescript
@@ -159,9 +158,7 @@ sum MySum
159158
| BranchN BranchNType1 ... BranchNTypeMN
160159
```
161160
162-
where the `...` denotes either an iterated `Branchi` for some `i`, or an
163-
iterated `BranchiTypej` for some `i` and `j`, then each branch, say `Branchi` is translated as
164-
follows.
161+
where the `...` denotes either an iterated `Branchi` for some `i`, or an iterated `BranchiTypej` for some `i` and `j`, then each branch, say `Branchi` is translated as follows.
165162
166163
- If `Branchi` has no fields i.e., `| Branchi`, then the corresponding TypeScript type's union member is
167164
@@ -177,8 +174,7 @@ follows.
177174
}
178175
```
179176
180-
where `<Product translation of BranchiType1 ... BranchiTypeMi>` denotes the
181-
right hand side of the [product translation](#product-types) of `prod FieldsProduct = BranchiType1 ... BranchiTypeMi`.
177+
where `<Product translation of BranchiType1 ... BranchiTypeMi>` denotes the right hand side of the [product translation](#product-types) of `prod FieldsProduct = BranchiType1 ... BranchiTypeMi`.
182178
183179
So, for example, given `| Branchi BranchiType1`, the corresponding TypeScript type is as follows
184180
@@ -197,8 +193,7 @@ follows.
197193
```
198194
199195
## Record types
200-
The types `Document` and `Chapter` have been declared as record types in the
201-
LambdaBuffers schema using the `record` keyword.
196+
The types `Document` and `Chapter` have been declared as record types in the LambdaBuffers schema using the `record` keyword.
202197
203198
Record types are mapped to [object types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types) in TypeScript.
204199
Given a LambdaBuffers' record type as follows
@@ -218,15 +213,15 @@ TypeScript has no builtin implementation of type classes. As such, LambdaBuffers
218213
A complete usage example can be found in the [TypeScript Prelude sample project](./typescript-prelude/src/index.mts).
219214
220215
A type class in TypeScript is an object type which defines a set of methods.
221-
For example, the `Eq` type class in Haskell defines `==` (equality) and `/=` (inequality) as follows.
216+
For example, the `Eq` type class in Haskell defines the set of methods `==` (equality) and `/=` (inequality) as follows.
222217
223218
```haskell
224219
class Eq a where
225220
(==) :: a -> a -> Bool
226221
(/=) :: a -> a -> Bool
227222
```
228223

229-
Thus, the [`Eq` class](https://github.com/mlabs-haskell/prelude-typescript/blob/main/src/Lib/Eq.ts) in TypeScript is:
224+
The corresponding [`Eq` class](https://github.com/mlabs-haskell/prelude-typescript/blob/main/src/Lib/Eq.ts) in TypeScript is:
230225

231226
```ts
232227
export interface Eq<A> {
@@ -302,9 +297,7 @@ This loosely follows the original translation given in the paper [How to make ad
302297

303298
## Limitations
304299

305-
- Only Haskell 2010 typeclasses are supported for the TypeScript code
306-
generator. So, the following schemas will probably generate incorrect
307-
code.
300+
- Only Haskell 2010 typeclasses are supported for the TypeScript code generator. So, the following schemas will probably generate incorrect code.
308301

309302
```purescript
310303
derive Eq a => Eq (MyPair a a)

0 commit comments

Comments
 (0)