GithubHelp home page GithubHelp logo

s-panferov / tsimmutable Goto Github PK

View Code? Open in Web Editor NEW
63.0 6.0 6.0 260 KB

TypeScript code generator for ImmutableJS

Home Page: https://www.npmjs.com/package/tsimmutable

TypeScript 87.54% JavaScript 12.46%

tsimmutable's Introduction

TypeScript code generator for ImmutableJS

Join the chat at https://gitter.im/s-panferov/tsimmutable

Codegen all you need to use ImmutableJS in your TypeScript application. Also uses type information to emit functions to parse nested Records.

Install

With npm do:

npm install tsimmutable --save-dev

Example

Write a file with your models (e.g. models.ts):

export interface Profile {
    firstName: string;
    lastName: string;
}

export interface User {
    profile: Profile;
    login: string;
    friends?: User[];
}

Compile it:

./node_modules/.bin/tsimmutable models.ts --out=models-imm.ts --indexerType=void --emitRecords --emitMarkers --emitEmptyRecords

See the output.

Options

--out               Output file name. Default: stdout
--keyType           Emit map with Map<$keyType, ...> type. Default: "string"
--indexerType       Emit map with Map<string, $indexerType> type. Default: "any"
--emitMaps          Shoud emitter emit Maps? Default: true
--emitRecords       Shoud emitter emit Records? Default: false
--emitMarkers       Shoud emitter emit type markers? Default: false
--emitEmptyRecords  Shoud emitter emit empty default records? Default: false
--emitTypedMethods  Shoud emitter emit typed methods? Default: false
--defaultEmptyType  Emit empty records with fields initializer by this value. Default: "null"

JS API

import { generate } from 'tsimmutable';
let result = generate(fileName, fileText, options);

Webpack plugin

tsimmutable goes together with webpack plugin which helps to watch and re-generate your model files when initial interface files change.

Usage:

var TsImmutablePlugin = require('tsimmutable/plugin');

module.exports = {
    resolve: {
        extensions: ['', '.ts', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'awesome-typescript-loader?module=common'
            }
        ]
    },
    entry: {
        index: ['./index.ts']
    },
    output: {
        path: './dist',
        filename: './[name].js'
    },
    plugins: [
        new TsImmutablePlugin({
            files: [
                './models.ts'
            ],

            /*
             * Every option below is optional.
             */

            suffix: '-i',
            verbose: true,
            indexerType: 'any',
            emitMaps: false,
            emitRecords: true,
            emitMarkers: true,
            emitEmptyRecords: true,
            emitTypedMethods: true,
            defaultEmptyType: 'null'
        })
    ]
};

Limitations

  1. Inline object types will not work:
export interface User {
    profile: {
        a: User
    };
}
  1. Two-dimensional array types will not work:
export interface User {
    profile: User[][]
}

License

MIT

tsimmutable's People

Contributors

gitter-badger avatar s-panferov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tsimmutable's Issues

This library doesn't produce the good output

Hi!

I tried to use this library which seems amazing, everything worked fine during a while, until I encounter problems. Then, I cannot use this library anymore.

Indeed, some definitions are wrong. For example, the merge function do not return a Map but the collection type. Moreover, when I create a record, I should be able to give a MyDataRecordDefaults, not a WorldRecordShape.

I give you this feedback in the hope you will have time to update and fix this tool.

Cheers

How to use in workflow

Thanks for this project, it's solving a big problem for me.

I can see how it works from glancing the project documentation, but I'm not sure how I would use this in my workflow. Do I compile my models manually and then import the output in my TypeScript application, or is there another way?

Hope you can help!
Olly

Work with `noImplicitAny: true`

Right now these two signatures cause an error because they are missing the any anotation:

function fromJSDefault(json) {
  // ...
}

function isPlainObj(value) {
  // ...
}

Would you be interested in a PR?

Doesn't work with generics

Hi Stanislav,

First of all. I really like the efforts you put into this project and I wanted to use it. However it doesn't seem to support generic types yet. Is that something you'd consider to fix?

Also have you looked into Typescript 2 (the readonly keyword in specific)?

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.