An oversimplification of the TypeScript Compiler API for defining and generating source files.

Overview

Tanu 🦝

A simplified abstraction of the TypeScript Compiler API for defining and generating source files.

npm GitHub issues GitHub Repo stars

Why?

I've always hated the fact that the majority of packages generate TypeScript files from a ridiculously long template literal string, It's not type safe or even readable at all, and I saw a cool tweet.

Yes Matt, It does exist now.

What does Tanu mean? 🦝

Tanuki (a cute animal) but I removed the last two characters cause tanuki was already taken on npm, and sounded cool enough for me. Naming things is hard, okay?

How do I use this?

const User = t.interface("User", {
  id: t.number(),
  email: t.string(),
  name: t.optional({
    first: t.string(),
    last: t.string(),
  }),
});

const MemberRole = t.enum("MemberRole", [
  "DEFAULT",
  "PRIVILEGED",
  "ADMINISTRATOR",
]);

const Member = t.interface("Member", {
  user: User,
  role: MemberRole,
});

const Organization = t.interface("Organization", {
  name: t.comment(t.string(), [
    "The organization name.",
    "@see https://example.com/organization-name",
  ]),
  description: t.optional(t.string()),
  members: t.array(Member),
});

const result = await t.generate([User, MemberRole, Member, Organization]);
console.log(result);
// the generated result.

export interface User {
  id: number;
  email: string;
  name?:
    | {
        first: string;
        last: string;
      }
    | undefined;
}
export enum MemberRole {
  DEFAULT,
  PRIVILEGED,
  ADMINISTRATOR,
}
export interface Member {
  user: User;
  role: MemberRole;
}
export interface Organization {
  /**
   * The organization name.
   * @see https://example.com/organization-name
   */
  name: string;
  description?: string | undefined;
  members: Array<Member>;
}

What about interfaces that reference themselves, or cross-reference each other?

Passing in a callback to the t.interface method will lazily populate the interface with its returned values. This will ensure that you can self-reference or cross-reference the interface, and it will be available by generation.

import { t } from "tanu.js";

const User = t.interface("User", () => ({
  users: t.array(User),
  posts: t.array(Post),
  authoredComments: t.array(Comment),
}));

const Post = t.interface("Post", () => ({
  author: User,
  text: t.string(),
  images: t.array(t.string()),
  postComments: t.array(Comment),
}));

const Comment = t.interface("Comment", {
  author: User,
  post: Post,
  text: t.string(),
});

const CommentReply = t.type("CommentReply", () => ({
  parent: CommentReply,
  author: User,
  post: Post,
  text: t.string(),
}));

const result = await t.generate([User, Post, Comment, CommentReply]);
console.log(result);
// the generated result

export interface User {
  users: Array<User>;
  posts: Array<Post>;
  authoredComments: Array<Comment>;
}
export interface Post {
  author: User;
  text: string;
  images: Array<string>;
  postComments: Array<Comment>;
}
export interface Comment {
  author: User;
  post: Post;
  text: string;
}
export type CommentReply = {
  parent: CommentReply;
  author: User;
  post: Post;
  text: string;
};
You might also like...

Lightweight and versatile build tool based on the esbuild compiler

Lightweight and versatile build tool based on the esbuild compiler

Estrella is a lightweight and versatile build tool based on the fantastic esbuild TypeScript and JavaScript compiler. Rebuild automatically when sourc

Jan 2, 2023

A "Basic-to-Lisp" compiler. But Basic is not real Basic, and Lisp is not real Lisp.

Basic2Lisp A "Basic-to-Lisp" compiler. But Basic is not real Basic, and Lisp is not real Lisp. Syntax Print-Sth Put some-value to standard output. PRI

Jul 10, 2022

Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Stable Diffusion UI Easiest way to install and use Stable Diffusion on your own computer. No dependencies or technical knowledge required. 1-click ins

Dec 30, 2022

A Compiler npm Package.

vcompiler 🎉 Version 1.x is live ! 🎉 Introducation It is the npm package for the compilation of the code. Currently it supports the following program

May 30, 2022

This is a library that makes it possible to change the configuration values of the Remix compiler (esbuild).

💽 remix-esbuild-override ⚠️ While I believe you will most likely get a lot of benefit from using this library, it can sometimes destroy your product.

Dec 22, 2022

The full power of the Go Compiler directly in your browser, including a virtual file system implementation. Deployable as a static website.

The full power of the Go Compiler directly in your browser, including a virtual file system implementation. Deployable as a static website.

Static Go Playground Features Full Go Compiler running on the browser. Supports using custom build tags. Incremental builds (build cache). Supports mu

Jun 16, 2022

Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs.

Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs.

WASI shim for AssemblyScript Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs. Note that this shim also serves a higher

Dec 23, 2022

The repository shows the compiler (simulator) of the Little Man Computer, which also contains some programs in the LMC programming language for implementing different functions.

Little Man Computer The repository shows the compiler (simulator) of the Little Man Computer, which also contains some programs in the LMC programming

Nov 17, 2022

Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collections

eznft Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collec

Sep 21, 2022
Comments
  • Add Hoisting to t.interface method, allowing cross-reference/recursive types.

    Add Hoisting to t.interface method, allowing cross-reference/recursive types.

    This allows for circular, and cross-references by leveraging the option to provide a callback, setImmediate, and Proxy.

    Code

    import { t } from "tanu.js";
    
    const User = t.interface("User", () => ({
      users: t.array(User),
      posts: t.array(Post),
      authoredComments: t.array(Comment),
    }));
    
    const Post = t.interface("Post", () => ({
      author: User,
      text: t.string(),
      images: t.array(t.string()),
      postComments: t.array(Comment),
    }));
    
    const Comment = t.interface("Comment", {
      author: User,
      post: Post,
      text: t.string(),
    });
    
    const result = await t.generate([User, Post]);
    console.log(result);
    

    Produces

    export interface User {
        users: Array<User>;
        posts: Array<Post>;
        authoredComments: Array<Comment>;
    }
    export interface Post {
        author: User;
        text: string;
        images: Array<string>;
        postComments: Array<Comment>;
    }
    export interface Comment {
        author: User;
        post: Post;
        text: string;
    }
    
    opened by ridafkih 1
  • Recursive types.

    Recursive types.

    Right now, this isn't currently possible.

    /**
     * 'User' implicitly has type 'any' because it does
     * not have a type annotation and is referenced directly 
     * or indirectly in its own initializer.
     */
    const User = t.interface("User", {
      name: t.string(),
      friends: t.array(User)
    });
    

    Within the current capabilities for the package, a workaround would be using t.reference("User") instead of directly referencing the variable. This works, but it's arguably more confusing.

    A potential solution for this issue is lazy evaluation (much like how Zod currently does it)

    const User: ts.InterfaceDeclaration = t.lazy(() =>
      t.interface("User", {
        name: t.string(),
        friends: t.array(User)
      })
    );
    
    enhancement help wanted good first issue 
    opened by ariesclark 1
  • Expand cross reference capabilities to type declaration

    Expand cross reference capabilities to type declaration

    This abstracts the lazy setting functionality by the Proxy, to type declarations as well.

    import { t } from "tanu.js";
    
    const User = t.interface("User", () => ({
      users: t.array(User),
      posts: t.array(Post),
      authoredComments: t.array(Comment),
    }));
    
    const Post = t.interface("Post", () => ({
      author: User,
      text: t.string(),
      images: t.array(t.string()),
      postComments: t.array(Comment),
    }));
    
    const Comment = t.interface("Comment", {
      author: User,
      post: Post,
      text: t.string(),
    });
    
    const CommentReply = t.type("CommentReply", () => ({
      parent: CommentReply,
      author: User,
      post: Post,
      text: t.string(),
    }));
    
    export interface User {
        users: Array<User>;
        posts: Array<Post>;
        authoredComments: Array<Comment>;
    }
    export interface Post {
        author: User;
        text: string;
        images: Array<string>;
        postComments: Array<Comment>;
    }
    export interface Comment {
        author: User;
        post: Post;
        text: string;
    }
    export type CommentReply = {
        parent: CommentReply;
        author: User;
        post: Post;
        text: string;
    };
    
    opened by ridafkih 0
Owner
Aries
Canadian software engineer 🇨🇦
Aries
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Fully-typed utilities for defining, validating and building your document

zhead Typed utilities for defining, validating and building best-practice document <head>'s. Status: Pre-release Please report any issues ?? Made poss

Harlan Wilton 70 Dec 21, 2022
This experimental library patches the global custom elements registry to allow re-defining or reload a custom element.

Redefine Custom Elements This experimental library patches the global custom elements registry to allow re-defining a custom element. Based on the spe

Caridy Patiño 21 Dec 11, 2022
A CLI for generating starter files for different JS frameworks with tailwindCSS pre-installed

tailwindcsscli A CLI for generating starter files for different JS frameworks with tailwindCSS pre-installed Installation To install the CLI, run the

Kira 12 Sep 30, 2022
Password Generator - A fast, simple and powerful open-source utility tool for generating strong, unique and random passwords

A fast, simple and powerful open-source utility tool for generating strong, unique and random passwords. Password Generator is free to use as a secure password generator on any computer, phone, or tablet.

Sebastien Rousseau 11 Aug 3, 2022
A Laravel Blade parser, compiler, and static analyzer written in TypeScript.

Blade Parser This library provides a Laravel Blade parser written in TypeScript. In addition to being able to parse Blade template files, this library

Stillat 7 Jan 4, 2023
A dockerfile to build ARM cross-compiler for Tauri (React Typescript)

tauri-arm A dockerfile to build a ARM cross-compiler for Tauri(React Typescript Template). MacOS Windows 10 Linux Setup $ yarn Installation Please bu

Shih-Cheng Huang 10 Sep 6, 2022
A markdown parser and compiler. Built for speed.

Marked ⚡ built for speed ⬇️ low-level compiler for parsing markdown without caching or blocking for long periods of time ⚖️ light-weight while impleme

Marked 28.9k Jan 7, 2023