将 Typescript 添加到 Coffeescript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12874942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Typescript to Coffeescript
提问by Greg Weber
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 转换为 typescript 再到 javascript。我的问题是:将类型签名添加到 coffeescript 函数的侵入性最小的方法是什么?
coffeescript supports raw javascript through backticks. However, that means coffeescript no longer understands the backtick snippet.
coffeescript 通过反引号支持原始 javascript。然而,这意味着 coffeescript 不再理解反引号片段。
Coffeescript rejects these:
Coffeescript 拒绝这些:
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.
它编译,但不进行类型检查。我认为这是因为 Coffeescript 已经声明了变量。
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
反引号在新的 Coffeescript Redux 编译器中似乎无法正常工作: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.
我很清楚这是一项可疑的努力,它现在只是一个实验。我目前使用的是 contract.coffee,但我正在寻找实际类型。
采纳答案by Tobiasz Cudnik
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.
这是我的项目,它将 CoffeeScript 转换为 TypeScript,然后将其与包含类型的 d.ts 文件合并。然后报告编译错误,如果有的话。
Its called Compiled-Coffee.
它称为Compiled-Coffee。
回答by Fenton
If you want to write CoffeeScript, it is best to write CoffeeScript and compile to JavaScript.
如果你想写CoffeeScript,最好先写CoffeeScript,然后编译成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.
TypeScript 的优势主要是设计时优势和更好的工具,因此在 CoffeeScript 和 JavaScript 中间使用它几乎没有什么好处,因为您将获得基于 CoffeeScript 代码的设计时间和工具。
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.
您可以在 TypeScript 中使用您在 CoffeeScript 中编写的库,反之亦然,因此您可以在 CoffeeScript 中维护您的 CoffeeScript 库,并在您决定走哪条路的同时在新的 TypeScript 文件中使用它们。
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.
TypeScript 确实是 JavaScript 的类型系统。作为工作流程早期的程序员,静态类型更适合您。在 IDE 中出现设计时警告意味着可以快速更正常见错误,例如输入错误的变量名称、错误的参数、无效的操作等等。用下划线和错误注释代码意味着即时反馈。在编译时使用这个很好,但你的反馈循环会更长。考虑到在使用 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:
至于所有“TypeScript vs CoffeeScript”评论——这个问题根本不是关于那个的。问题是关于从 CoffeeScript 编译到 TypeScript,然后再到 JavaScript。让我们看看为什么这可能不理想:
- 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
- 你只会在编译时得到类型反馈
- 你不会得到自动完成
- 您的 CoffeeScript 代码将不再紧凑——它将具有类型注释
- 如果没有中间编译器,您的 CoffeeScript 代码将不再有效
- 您将不得不使用额外的编译器,它需要与 CoffeeScript 版本 x 和 TypeScript 版本 y 同步
- 您的 IDE 无法理解您的 CoffeeScript 代码
回答by Greg Weber
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.
我认为我想出的东西是我能做的最好的。在新的 Coffeescript Redux 编译器中,事情变得更难了:尝试破解当前的 coffeescript 编译器以使其工作实际上会更容易。
The way of making this look less hacky is:
使这看起来不那么hacky的方法是:
`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.
但是,我仍然不确定如何使用此表单指定返回值。
回答by Riceball LEE
Typescript is a strong typejavascript. 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.
Typescript 是一种强类型的javascript。Coffee-script 提供了一种更舒适的书写和阅读方式。我不把咖啡脚本当作一种语言。这只是一种方式,一种可以附加到任何语言的风格。
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 CoffeeScriptReduxsource to add the strong type supported
- the TypedCoffeeScripthas already done.
- Modify the Typescript parser source to use coffee-script syntax.
- It seems nobody do this.
- 修改CoffeeScriptRedux源码,添加支持的强类型
- 该TypedCoffeeScript已经完成。
- 修改 Typescript 解析器源以使用 coffee-script 语法。
- 似乎没有人这样做。

