Typify Type Parser

1.1.0 · abandoned · verified Tue Apr 21

typify-parser is a JavaScript library designed to parse declarative type signature strings, primarily intended for use within the broader `typify` ecosystem for runtime type-checking. It takes human-readable type declarations, such as `(foo, bar 42) -> quux`, and transforms them into a structured JSON Abstract Syntax Tree (AST) representation. This AST details the type components, including function types, product types, identifiers, and literal values like numbers. Additionally, it provides utility methods like `freeVars` to extract unbound type variables from the parsed structure. The package is currently at version `1.1.0`, but it was last published in July 2015, indicating it is no longer actively maintained. Its parent project, `typify`, also appears to be dormant. Developers considering this package should be aware of its abandoned status and the potential compatibility issues with modern JavaScript environments and module systems. Its core differentiation was its ability to convert custom type signature strings into a machine-readable format for subsequent type validation by `typify`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a type signature string into its Abstract Syntax Tree (AST) representation and how to extract free variables using the `freeVars` utility.

const parser = require('typify-parser');

// Example: Parse a complex type signature into an AST
const typeSignatureString = "(name: string, age: number, ?email: string) -> User";
const ast = parser(typeSignatureString);

console.log('Parsed AST:', JSON.stringify(ast, null, 2));

// Example: Parse another signature and extract free variables
const freeVarsAST = parser("rec list -> () | a & list");
const variables = parser.freeVars(freeVarsAST);

console.log('\nFree variables for "rec list -> () | a & list":', variables);

/* Output will resemble:
Parsed AST: {
  "type": "function",
  "arg": {
    "type": "product",
    "args": [
      {
        "type": "ident",
        "value": "name",
        "alias": {
          "type": "ident",
          "value": "string"
        }
      },
      {
        "type": "ident",
        "value": "age",
        "alias": {
          "type": "ident",
          "value": "number"
        }
      },
      {
        "type": "ident",
        "value": "email",
        "alias": {
          "type": "ident",
          "value": "string"
        },
        "optional": true
      }
    ]
  },
  "result": {
    "type": "ident",
    "value": "User"
  }
}

Free variables for "rec list -> () | a & list": [
  "a"
]
*/

view raw JSON →