0.7.8 • Published 10 years ago

typescript-schema v0.7.8

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

TypeScript Schema Library

Overview

A reflective type model for TypeScript applications. There are two primary models:

  • A raw model, which is a serialisable format (consider using typescript-package for the generation of this from a TypeScript package or project). References to types are stored as a {moduleName:string, name:string} object.
  • A fully resolved model, where all references are replaced with actual instances of that type. This is the model most useful for use.

A high level over-view of the modules in this library are:

  • rawConverter: converts raw models to resolved models.
  • model: contains the type meta-model, both for raw and resolved models
  • factories: used by rawConverter. Contains the code for creating resolved model elements
  • equals: contains equality functions for resolved models
  • typeCloser: elements with type-parameters (classes, interfaces, etc.) are treated as type-constructors. To create an actual type, use typeCloser with arguments for the parameters
  • typeUtils: some useful functions for dealing with types
  • serialiserParser: use this to create a more readable serialised form of a raw model (and for parsing that form)
  • filter: used to reduce raw models to complete sub-models based off specific elements (e.g.: filtered to a closed model for a particular module, or class)
  • toString: used to create a unique identifier for various resolved model elements
  • expressionToLiteral: used to convert a resolved expression model into a pure javascript equivalent
  • visitor: used to easily navigate a resolved model

Usage

Install:

npm install typescript-schema

Basic Usage:

import * as s from 'typescript-schema'

let rawModules:s.Map<s.Module>

// Given a raw model, filter its contents to only contain types relevant to and referenced by some module
let filteredModules = s.filterRawModules(['moduleA'], rawModules)

// Given a raw model, convert to a resolved one:
let resolvedModules = s.convertRawModules(rawModules)

// Give a raw model, create a nice text format
let str = s.stringify(rawModules)
// And parse it back into a raw model
rawModules = s.parse(str)

let someInterfaceConstructor:s.InterfaceConstructor
let someTypeForT:s.Type
// Given a type constructor (e.g. interface A<T>), create a type
s.closeInterface(someInterfaceConstructor, [someTypeForT])

// Visit the elements in a model
s.modulesVisitor(resolveModules, {
  onModule: function(module){
    return <s.ModuleVisitor> {
      onClassConstructor: function(cc) {
        // etc
      }
    }
  }
})

// Get the unique id for an element
let id = s.interfaceConstructorToString(someInterfaceConstructor)

let decorator:s.Decorator

// For some expression (e.g. from a decorator)
let pureValue = s.expressionToLiteral(decorator.parameters[0])

let someOtherInterfaceConstructor

// Compare two entities
let isEqual = someInterfaceConstructor.equals(someOtherInterfaceConstructor)
0.7.8

10 years ago

0.7.7

10 years ago

0.7.6

10 years ago

0.7.5

10 years ago

0.7.4

10 years ago

0.7.3

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago