Hyperlinkv0.8.0-beta.28

SchemaAST

SchemaAST.toTypeconsteffect/SchemaAST.ts:3394
<A extends AST>(ast: A): A

Strips all encoding transformations from an AST, returning the decoded (type-level) representation.

Details

  • Memoized: same input reference → same output reference.
  • Recursively walks into composite nodes (Arrays, Objects, Union, Suspend).

Example (Getting the type AST)

import { Schema, SchemaAST } from "effect"

const schema = Schema.NumberFromString
const typeAst = SchemaAST.toType(schema.ast)
console.log(typeAst._tag) // "Number"
export const toType = memoize(<A extends AST>(ast: A): A => {
  if (ast.encoding) {
    return toType(replaceEncoding(ast, undefined))
  }
  const out: any = ast
  const type = out.recur?.(toType) ?? out
  const encodingChecks = type.encodingChecks
  if (encodingChecks) {
    return modifyOwnPropertyDescriptors(type, (d) => {
      d.encodingChecks.value = undefined
      if (type === ast) {
        d.checks.value = combineChecks(type.checks, encodingChecks)
      }
    })
  }
  return type
})
Referenced by 5 symbols