Skip to content

FartLabs/typescript-type-introspector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typescript-type-introspector

Reason with TypeScript code via type introspection, powered by Tree-Sitter.

Introspection

Introspection helps avoid the problem of slow types introduced by libraries like Zod and TypeBox. It allows you to determine the type of a value at runtime, rather than inferring it at compile time. This can lead to more efficient and dynamic type handling in your TypeScript projects.

Installation

{
  "imports": {
    "typescript-type-introspector": "https://deno.land/x/introspector"
  }
}

TypeBox

TypeBox objects are JSON Schema objects with extra in-memory magic!

A fun way to think about TypeBox is that it does for JSON Schema what drizzle does for SQL - it’s a nice, TypeScript-friendly builder syntax for an existing spec that tries to expose all of the spec’s power. That’s not all TypeBox is - it has an extremely fast built-in type checker, it can create example values based on schemas, and a lot more.

- https://blog.val.town/blog/typebox/

Introspection helps avoid the problem of slow types introduced by libraries like Zod and TypeBox. It allows you to determine the type of a value at runtime, rather than inferring it at compile time. This can lead to more efficient and dynamic type handling in your TypeScript projects.

TypeBox example

Instantiate a TypeBox schema at runtime using introspection via @sinclair/typebox/compiler.

import { assert } from "@std/assert";
import { TypeCompiler } from "@sinclair/typebox/compiler";
import {
EXAMPLE_PERSON,
EXAMPLE_PERSON_INTROSPECTION,
} from "#/lib/example/person.ts";
import { schemaFromIntrospection } from "#/lib/typebox/schema-from-introspection.ts";
Deno.test("schemaFromIntrospection introspects TypeScript class", () => {
const schema = schemaFromIntrospection(EXAMPLE_PERSON_INTROSPECTION);
const typeCheck = TypeCompiler.Compile(schema);
assert(typeCheck.Check(EXAMPLE_PERSON));
});

Development

Make sure to install Deno: https://deno.land/manual/getting_started/installation.

Run the tests:

deno task dev

Format the project:

deno fmt

Check for common errors:

deno lint

Developed with 🧪 @FartLabs

About

Reason with TypeScript code via type introspection.

Resources

License

Code of conduct

Stars

Watchers

Forks