Expressions
Literals and names
c.str("a") // "a"
c.num(1) // 1
c.nul() // null
c.undef() // undefined
c.id("db") // a chain that starts at the identifier db
Chains
c.id(name) starts a chain: an immutable value with four steps. Each step returns a new chain. Call .build() to get the expression.
| Step | Result |
|---|---|
.member("name") | .name, or ["name"] when name is not a valid identifier |
.index(expr) | [expr] |
.call(...args) | (args) |
.method("name", ...args) | .name(args) |
c.id("db").method("selectFrom", c.str("users")).method("selectAll").build()
// db.selectFrom("users").selectAll()
c.path("a", "b", "c") is a.b.c. c.instantiate(expr, types) gives expr<T> and c.callWithTypes(callee, types, args) gives callee<T>(args).
Operators
c.strictEq, c.strictNe, c.and(...terms), c.ternary(test, a, b), c.assign(name, value), c.assignMember(object, name, value), c.newOf(callee, ...args), c.spread(expr) and c.yieldStar(expr). c.and() with no terms gives true. c.yieldStar(effect) is yield* effect, the Effect generator bind.
Objects and arrays
c.obj().prop("id", c.num(1)).shorthand("name").spread(c.id("rest").build()).build()
// { id: 1, name, ...rest }
c.array(c.num(1), c.spread(c.id("more").build()))
// [1, ...more]
prop quotes a key that is not a valid identifier.
Functions
c.fn() returns a builder. The steps are param, optionalParam, destructured, returns, body, expr and generator.
c.fn()
.param("id", c.ts.string)
.returns(c.ts.ref("Promise", c.ts.number))
.body(c.ret(c.num(1)))
.build()
// (id: string): Promise<number> => { return 1; }
.expr(expression) makes an arrow function with an expression body. .generator() builds a function* expression, which an Effect program uses with c.yieldStar.
Template literals
c.tagged(tag, ...parts) builds a tagged template from text and expressions. Text is escaped; each expression becomes a ${...} hole.
c.tagged(c.id("sql").build(), "select * from users where id = ", c.id("id").build())
// sql`select * from users where id = ${id}`