Typify Type Parser
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
- breaking The `typify-parser` package has been abandoned since its last publish in July 2015. It is no longer actively maintained, meaning there will be no new features, bug fixes, or security updates.
- gotcha This package predates widespread ES Modules adoption and only provides a CommonJS `require()` interface. It does not natively support `import` statements.
- gotcha The package specifies a Node.js engine requirement of `>= 0.10.0`, an extremely old version. It has not been tested or updated for compatibility with modern Node.js releases (e.g., Node.js 16+ or 18+).
Install
-
npm install typify-parser -
yarn add typify-parser -
pnpm add typify-parser
Imports
- parser
const parser = require('typify-parser').parser;import parser from 'typify-parser';
- parser
const parser = require('typify-parser'); - freeVars
import { freeVars } from 'typify-parser';const { freeVars } = require('typify-parser');
Quickstart
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"
]
*/