Code Monkey home page Code Monkey logo

ts-to-goog's Introduction

ts-to-goog

A tool that converts TypeScript definitions into Closure Compiler externs.

Motivation

Consuming foreign JavaScript libraries became piece of cake with latest version of ClojureScript, but you have to still provide externs when compiling code in advanced mode. ClojureScript can infer a lot itself, but definitely not everything, which is the point where you have to write the externs manually.

Last year I spent a lot of time with TypeScript language, a typed superset of JavaScript that compiles to JavaScript. While in ClojureScript you have to provide appropriate externs only if you want to compile in advanced mode, in TypeScript you are forced to provide externs, they call them definitions, even for development. Otherwise you won't be able to compile.

TypeScript community therefore maintens DefinitelyTyped, a resource with ~3500 high quality definitions. Also in JavaScript community it's good practice to provide TypeScript definitions even if the package itself is in pure JavaScript.

While working in TypeScript I came accross tsickle, TypeScript to Closure Translator, and I got the idea - Could it be used to convert TypeScript definitions into Closure externs?

Usage

  1. wget -O gen.jar https://honzabrecka.com/public/generator-0.1.0-SNAPSHOT-standalone.jar
  2. java -jar gen.jar <npm-package> eg. java -jar gen.jar @types/react
  3. Enjoy generated extern in externs directory.

The state of the converter

As a proof of concept I converted all definitions from DefinitelyTyped. Results can be found in externs directory.

First version was just a bash script as I wanted to validate my idea quickly. Then I rewrote it to Clojure, because it's meant to be used in ClojureScript world, where ClojureScript compiler itself and tooling around ClojureScript are written in Clojure and therefore JVM is standard equipment.

Currently we are disscussing integration with existing tooling, namely cljsjs. I tried to summarize my thoughts on this topic:

Ts-to-goog takes an advantage from already existing TypeScript's ecosystem. It means that every single package with "typings" (written in TypeScript or providing TypeScript definition file) published on npm should be convertible and therefore reused in ClojureScript ecosystem. This is crucial as it differs from how cljsjs currently works.

Cljsjs packages provides both :externs and :foreign-libs, where both files require manual maintenance. It's completely separated from npm, which means that when the new version of npm package is published, cljsjs package is out of date and there's no automated mechanism to update it.

Also even though that the user who wants to update may reuse :externs from that outdated package, :foreign-libs may be something not-so-easy to update as it requires compilation on user's environment and each library comes with its own setup...

This point became invalid with recent version of ClojureScript that brings first-class support for :npm-deps. As it can consume npm modules directly, there's no need for :foreign-libs in cljsjs packages, which means that update is quite easy.

This is exactly how DefinitelyTyped is designed. To use a library you have to install both packages, one for library and second for types.

Here's my suggestion:

  • cljsjs packages generated from TypeScript defs would contain just :externs.
  • Those packages should have ts namespace in its name, eg. cljsjs.ts.react
  • We can build a service that would periodically check for new releases on npm and convert&publish them automatically to clojars to remove any redundant manual work.

Contributing

All help is appreciated. Please open an issue or tweet at me if you want some help getting started, or just want to discuss ideas.

ts-to-goog's People

Contributors

honzabrecka 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

Watchers

 avatar  avatar  avatar

Forkers

mbrukman

ts-to-goog's Issues

Clarify licensing of extern files

This repository contains works derived from the contents of the definitelyTyped repository (the extern files). However most of the original files are under MIT license and copyright information as well as other licensing information from the .d.ts files have not been preserved in this repository and the new files are now being redistributed here under EPL.

IANAL, but I don't think this is OK and indeed I do represent one such copyright holder for which in this repository there is one such derived piece of work which should not be distributed that way without being attributed, correctly.

Please clarify that the license and copyrights in this repository only applies to the code for the converter, but not to the generated (mostly outdated anyway) extern files which have also been committed to this repository without proper attribution and copyright notice.

To make this clear: I personally have no intention to sue or whatever. I encourage you to continue your work on this and similar projects and I don't want you to take it down or change it. It's just that people have been pointing at your repo as a "proof" that our copyrighted software is actually available under terms that they have never been released under, just because they are part of this repository. A simple clarification that the files in "externs" have been taken from the definitely typed repository and that users should go there to find out more about the original licensing and the copyright holders for the respective files would be all I ask for.

Thanks!

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.