npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-rtti

v0.0.1

Published

type script run time type information (plugin for ttypescript)

Downloads

6

Readme

ts-rtti

ts-rtti enables typescript static type information to be used for run time type checking.

(Coming soon.)

Interface

It is provided as a plugin for use with ttypescript.

Implementation

Type script type information is parsed and converted opaquely to avj schemas.

The schemas are by default converted to ajv codegen format, which allows for fast execution, both on the server side (node) and on the broswer.

Features

Types which can be parsed

Files are selected and within those files top level functions are selected for targeting. The parameter list of those selected functions is treated as a top level ts-rtti run time type.

ts-rtti handles typescript native types:

  • Primitives
    • number
    • string
    • bigint
    • symbol
    • constant primitve values (excepting symbols)
  • array
  • object
  • function (ingoring signature)
    • Exception: Selected top level functions are parsed so they can be used as validators of their own arguments.
  • class
    • Currently instanceof is not implemented. Classes are treated no differently than the interfaces they implement.
  • Typescript union (|)
    • Currently union key merging is propogated only one level deep, whereas typescript goes infinitely deep.
  • Typescript intersection (&)
  • All other Typescript types: typescript utilities and user defined types.

ts-rtti allows, via an optional configuration setting, union (|) and intersection (&) to be interpreted as logical operators that map one-to-one with json schema anyof and allof operators. (This is a global setting, so not generally useful - but there could be specific limited use cases.)

Recurrent Typescript types are handled correctly. ts-rtti handles recursion during parsing, and ajv allows recurrent schema definitions.

Note: Input data structure recurrence (the data being validated) is handled if and only if ajv handles that. A recurrent type definition does not imply the corresponding input data must be recurrent. In general it is not.

ttypesctipt Compilation

ts-rtti module supplied as a plugin which is consumable by ttypescipt. There are some intermediate typescript source files created by the ts-rtti plugin which contain the ajv generated code and related interface, all necessary for runtime use.
By default, these typescript source files are not written to disk but are converted from memory directly to javascript output, along with rest of the users compiled output.

However, the user may optionally enable the generation to disk of the ts-rtti intermediate typescript files. Then the entirity could later be compiled without use ttypescipt, just using ordinary typescript instead. (In that case, an extra step is required to deliberately set the import path to the ts-rtti intermediate typescript files.)

ajv schemas.

As mentioned previously, by default the ajv schemas are converted to codegen form, which is faster, and can also be used on the browser. However, the ajv schema can optionally be saved and used. The codegen can also be optionally turned off.

Example usage

Future feature candidates

  • Parsing of arbitrary top level types, rather than just allowing function signatures.
  • Hybrid types that allow blending of typescript types and explicit json-schema structures.
    • This would also be a way to implement logical-stype union and interesection in typescript, although the static typing would be missing.
  • Custom keys to allow pinpoint validiation conditions.
  • Enable export of selected ajv schemas as self-contained output files.
  • Faithful computation of typescript union (|), allowing arbitrarily deep object type key merging.
    • Upside: Although this is not possible to do in O(1) size using the normal JSON schema interface, it is possible to implement a virtual stack in parallel to ajv runtime, and connected to that runtime by means of custom ajv keys, allowing both O(1) code size and O(1) execution time (relative to Typescript native union computation).
    • Downside: But would there actually a demand for that? The current one-level deep object type key merging may be enough.
  • Edit time / language server usage of json schema validation to validate user input when the ts-rtti validation conditions are stronger than (or different from) the conditions (if any) imposed by native Typescipt. This would be ts-rtti used for static typing.
    • Downside: A lot of work. Also, there is a complete iteration of programCreate between edit of code upon which some json-schema depends, and the update of that json-schema. That could lead to ungainly sluggish UI in those cases.
    • Upside: It could be very useful for broading type available type syax, e.g., for allowing logical-style union and intersection of object types to get equal treatment under UI.