Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-poet

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-poet

code generation DSL for TypeScript

  • 6.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
448K
decreased by-6.57%
Maintainers
1
Weekly downloads
 
Created

What is ts-poet?

The ts-poet npm package is a TypeScript code generation library that allows you to programmatically create TypeScript code. It is particularly useful for generating code based on some input data or schema, such as generating TypeScript types from a GraphQL schema or a protobuf definition.

What are ts-poet's main functionalities?

Generating TypeScript Interfaces

This feature allows you to generate TypeScript interfaces programmatically. The code sample demonstrates how to create a 'User' interface with properties 'id', 'name', and 'email'.

const { Code, InterfaceSpec } = require('ts-poet');

const userInterface = InterfaceSpec.create('User')
  .addProperty('id', 'number')
  .addProperty('name', 'string')
  .addProperty('email', 'string');

const code = Code.create(userInterface);
console.log(code.toString());

Generating TypeScript Classes

This feature allows you to generate TypeScript classes programmatically. The code sample demonstrates how to create a 'User' class with properties 'id', 'name', and 'email', and a constructor method.

const { Code, ClassSpec } = require('ts-poet');

const userClass = ClassSpec.create('User')
  .addProperty('id', 'number')
  .addProperty('name', 'string')
  .addProperty('email', 'string')
  .addMethod('constructor', 'constructor(id: number, name: string, email: string) { this.id = id; this.name = name; this.email = email; }');

const code = Code.create(userClass);
console.log(code.toString());

Generating TypeScript Functions

This feature allows you to generate TypeScript functions programmatically. The code sample demonstrates how to create a 'greet' function that takes a 'name' parameter and returns a greeting string.

const { Code, FunctionSpec } = require('ts-poet');

const greetFunction = FunctionSpec.create('greet')
  .addParameter('name', 'string')
  .setReturnType('string')
  .setBody('return `Hello, ${name}!`;');

const code = Code.create(greetFunction);
console.log(code.toString());

Other packages similar to ts-poet

FAQs

Package last updated on 23 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc