Code Monkey home page Code Monkey logo

ts-type-info's Introduction

TSTypeInfo

Build Status Coverage Status

Reflection and code generation in TypeScript.

Uses the TypeScript Compiler API to get the type and structure information of TypeScript code in an easily usable format.

npm install ts-type-info --save-dev
tsd link

Reflection

// V:/TestFile.ts

function myDecorator(str: string) {
    return (target: typeof MyClass) => {
        target.myStaticProperty = str;
    };
}

@myDecorator("My decorator value")
export class MyClass {
    static myStaticProperty: string | number;

    myProperty = 253;

    myMethod(myParameter: string) {
        return `Test: ${myParameter}`;
    }
}

Get the file info:

import * as TsTypeInfo from "ts-type-info";

const files = TsTypeInfo.getFileInfo(["V:/TestFile.ts"]);
const myPropertyName = files[0].classes[0].properties[0].name;

console.log(myPropertyName); // myProperty

Code Generation

// V:/TestFile2.ts

class MyClass {
    myMethod(str: string) {
    }
}

Get the file info and tell it how it should output:

import * as TsTypeInfo from "ts-type-info";

const files = TsTypeInfo.getFileInfo(["V:/TestFile2.ts"]);
const myClass = files[0].classes[0];

myClass.isAbstract = true;
myClass.onBeforeWrite = writer => writer.write("@MyDecorator");
myClass.methods[0].onBeforeWrite = writer => writer.write("// myMethod is here");
myClass.methods[0].onWriteFunctionBody = writer => {
    writer.write(`if (str != null && str.length > 40)`).block(() => {
        writer.write("alert(str)");
    });
    writer.newLine().write("return str;");
};

console.log(myClass.write());

Outputs:

@MyDecorator
abstract class MyClass {
    // myMethod is here
    myMethod(str: string) {
        if (str != null && str.length > 40) {
            alert(str);
        }

        return str;
    }
}

Real Life Example

  • Server Bridge - Automatically generates client side code to communicate with the server from the server side code.

ts-type-info's People

Contributors

dsherret avatar frankandrobot avatar

Stargazers

Mateusz Tamborek avatar

Watchers

James Cloos avatar Vincent Pang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.