简体   繁体   English

将打字稿转换为javascript

[英]Converting typescript to javascript

Is there currently any converters online that could convert ts to js?目前网上有没有可以将ts转js的转换器? I would like to use the components from here , but they're all written in ts and my rails app doesn't support it.我想使用这里的组件,但它们都是用 ts 编写的,我的 rails 应用程序不支持它。

for example this file例如这个文件

import { Controller } from "@hotwired/stimulus"
import { useTransition } from 'stimulus-use/dist/use-transition'

export default class extends Controller {
  menuTarget: HTMLElement
  toggleTransition: (event?: Event) => void
  leave: (event?: Event) => void
  transitioned: false

  static targets = ['menu']

  connect (): void {
    useTransition(this, {
      element: this.menuTarget
    })
  }

  toggle (): void {
    this.toggleTransition()
  }

  hide (event: Event): void {
    // @ts-ignore
    if (!this.element.contains(event.target) && !this.menuTarget.classList.contains('hidden')) {
      this.leave()
    }
  }
}

what kind of steps should I do in order to convert this to js?我应该采取什么样的步骤才能将其转换为 js? Bear in mind that I know nothing about typescript so I'm getting little confused here.请记住,我对打字稿一无所知,所以我在这里有点困惑。

What I've currently done is the following我目前所做的是以下

import { Controller } from "@hotwired/stimulus"
import { useTransition } from 'stimulus-use/dist/use-transition'

export default class extends Controller {
  menuTarget: HTMLElement
  toggleTransition: (event?: Event) => void
  leave: (event?: Event) => void
  transitioned: false

  static targets = ['menu']

  connect() {
    useTransition(this, {
      element: this.menuTarget
    })
  }

  toggle() {
    this.toggleTransition()
  }

  hide(event) {
    // @ts-ignore
    if (!this.element.contains(event.target) && !this.menuTarget.classList.contains('hidden')) {
      this.leave()
    }
  }
}

but I don't quite know what to do with the hide function since it depends on the lines但我不太清楚如何处理隐藏功能,因为它取决于线条

menuTarget: HTMLElement
toggleTransition: (event?: Event) => void
leave: (event?: Event) => void
transitioned: false

Option 1: Use the Typescript Compiler ( tsc )选项 1:使用 Typescript 编译器 ( tsc )

If you only need to do this once and you're not going to update this code anytime soon, then one easy way is to use the typescript compiler directly.如果您只需要这样做一次并且您不会很快更新此代码,那么一种简单的方法是直接使用 typescript 编译器。

(I am assuming you have Node and npm installed on your machine): (我假设你的机器上安装了 Node 和npm ):

  1. First download your files from that repo to a directory.首先将您的文件从该存储库下载到一个目录。
  2. Then, inside that directory, go and run npm i -D typescript然后,在该目录中,运行npm i -D typescript
  3. Generate a basic tsconfig.json via: npx tsc --init通过 npx npx tsc --init生成一个基本的tsconfig.json
  4. Then call the typescript compiler: npx tsc --outDir ./build然后调用 typescript 编译器: npx tsc --outDir ./build

Now you have all the javascript ES5 versions of these files in the build directory.现在,您在build目录中拥有了这些文件的所有 javascript ES5 版本。

To explain what the last command does:解释最后一个命令的作用:

  • npx is a way to invoke installed npm binaries. npx是一种调用已安装的 npm 二进制文件的方法。 It's effectively a package runner.它实际上是一个包运行器。
  • tsc is the typescript compiler as a binary tsc是作为二进制文件的打字稿编译器
  • --outDir is the command line flag to indicate where to output the files. --outDir是指示文件输出位置的命令行标志。

So if your files looked like this:因此,如果您的文件如下所示:

foo.ts
bar.ts

After that command, it will be:在该命令之后,它将是:

build/
  foo.js
  bar.js
foo.ts
bar.ts

If you want to modify the output options, I would suggestreading the docs on tsconfig here如果您想修改输出选项,我建议您在此处阅读 tsconfig 上的文档

Option 2: Use a bundler like Rollup选项 2:使用像 Rollup 这样的打包工具

If you want to just have this be done for you and use standard package management, I would suggest looking into integrating Rollup or Webpack.如果您只想为您完成此操作并使用标准包管理,我建议您考虑集成 Rollup 或 Webpack。

Since you're using Rails, I would suggest looking into something like Webpacker which will allow you to use Typescript in your Rails app由于您使用的是 Rails,我建议您研究类似Webpacker 的东西,它允许您在 Rails 应用程序中使用 Typescript

This is a much better option if you plan on keeping this code updated with wherever you're getting it.如果您计划随时更新此代码,这是一个更好的选择。

What IDE are you using?你用的是什么IDE? RubyMine, Visual Studio, etc. do support automatic TypeScript to Javascript transpiling. RubyMine、Visual Studio 等确实支持将 TypeScript 自动转译为 Javascript。 So you can work in TypeScript and in the background the Javascript files are automatically updated.因此,您可以使用 TypeScript 并在后台自动更新 Javascript 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM