Greg Weber
Greg Weber

Reputation: 3428

Adding Typescript to Coffeescript

I have a build chain setup that will convert a file from coffeescript to typescript to javascript. My question is: what is the most minimally intrusive way to add type signatures to a coffeescript function?

coffeescript supports raw javascript through backticks. However, that means coffeescript no longer understands the backtick snippet.

Coffeescript rejects these:

f = (`a:String`) -> a + 2
f = (a`:String`) -> a + 2

I can write this above the function:

`var f = (String) => any`

It compiles, but does not do the type-checking. I think this is because Coffeescript already declared the variable.

The only way I could figure out how to make it work requires a lot of boilerplate

f = (a) ->
  `return (function(a:String){`
  a + 2;
  `})(a)`

Backticks do not seem to work properly in the new Coffeescript Redux compiler: https://github.com/michaelficarra/CoffeeScriptRedux/issues/71

I am well aware that this is a dubious endeavor, it is just an experiement right now. I currently use contracts.coffee, but I am looking for actual types.

Upvotes: 23

Views: 10413

Answers (4)

Riceball LEE
Riceball LEE

Reputation: 1591

Typescript is a strong type javascript. Coffee-script provides a more comfortable way of writing and reading. I do not treat coffee-script as a language. It's just a way, a style that can be attached to any language: Coffee Style Smart Computer Language should be the future

It's very ugly and stupid through backtick to 'support' the such strong type. The correct way to implement the coffee-script with strong type:

  • Modify the CoffeeScriptRedux source to add the strong type supported
  • Modify the Typescript parser source to use coffee-script syntax.
    • It seems nobody do this.

Upvotes: 2

Tobias Cudnik
Tobias Cudnik

Reputation: 9630

Here's my project which transpiles CoffeeScript into TypeScript and then merges it with a d.ts file containing types. Then reports compilation errors, if any.

Its called Compiled-Coffee.

Upvotes: 21

Fenton
Fenton

Reputation: 251062

If you want to write CoffeeScript, it is best to write CoffeeScript and compile to JavaScript.

The benefit of TypeScript is mostly design-time benefit and better tooling, so using it in the middle of CoffeeScript and JavaScript adds very little benefit as you will get design time and tooling based on your CoffeeScript code.

You can consume the libraries you write in CoffeeScript in TypeScript and vice-versa, so you can maintain your CoffeeScript libraries in CoffeeScript and consume them in your new TypeScript files while you decide which way to go.

Update: I'm not sure how there can be such a wide misinterpretation of this answer - I'm going to assume that I haven't explained this well (rather than assuming it is merely straw-man argument or hyper-sensitivity to language comparison).

TypeScript is indeed a type system for JavaScript. Static types are more use to you as a programmer earlier in the workflow. Having design-time warnings in your IDE means rapid correction of common errors like mis-typed variable names, incorrect parameters, invalid operations and a whole lot more. Having code underlined and annotated with an error means instant feedback. Having this at compile-time is good, but your feedback loop is longer. I won't even talk about run-time given that all types are erased by this point when using TypeScript.

As to all the "TypeScript vs CoffeeScript" comments - this question is not about that at all. The question is about compiling from CoffeeScript to TypeScript and then to JavaScript. Let's look at why this might not be ideal:

  • You will only get type feedback at compile time
  • You won't get auto-completion
  • Your CoffeeScript code will no longer be compact - it will have type annotations
  • Your CoffeeScript code will no longer be valid without your intermediate compiler
  • You will have to use an additional compiler and it will need to be in-step with CoffeeScript version x and TypeScript version y
  • Your IDE won't understand your CoffeeScript code

Upvotes: 17

Greg Weber
Greg Weber

Reputation: 3428

I think what I came up with is the best I can do. Things are harder in the new Coffeescript Redux compiler: it would actually be easier to try to hack the current coffeescript compiler to make this work.

The way of making this look less hacky is:

`var f : (a:Number) => Number = originalF`

However, typescript's weak type inference doesn't do that well with this form. This gets proper type analysis:

f = (a) ->
  `var a : Number = a`
  a + 2

However, I am still not sure how to specify a return value with this form.

Upvotes: 2

Related Questions