3

GitHub - ariesclark/tanu.js: An oversimplification of the TypeScript Compiler AP...

 2 years ago
source link: https://github.com/ariesclark/tanu.js
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Tanu raccoon

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

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.

68747470733a2f2f692e696d6775722e636f6d2f356f444c654a6e2e706e67

Yes Matt, It does exist now.

What does Tanu mean? raccoon

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>;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK