admin管理员组文章数量:1332359
Is there currently any converters online that could convert ts to js? I would like to use the ponents from here, but they're all written in ts and my rails app doesn't support it.
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? 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
Is there currently any converters online that could convert ts to js? I would like to use the ponents from here, but they're all written in ts and my rails app doesn't support it.
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? 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
Share
Improve this question
edited Jun 11, 2022 at 2:03
xpt
23.1k45 gold badges153 silver badges245 bronze badges
asked Oct 24, 2021 at 12:42
DanielDaniel
1911 gold badge1 silver badge8 bronze badges
2 Answers
Reset to default 6Option 1: Use the Typescript Compiler (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 piler directly.
(I am assuming you have Node and npm
installed on your machine):
- First download your files from that repo to a directory.
- Then, inside that directory, go and run
npm i -D typescript
- Generate a basic
tsconfig.json
via:npx tsc --init
- Then call the typescript piler:
npx tsc --outDir ./build
Now you have all the javascript ES5 versions of these files in the build
directory.
To explain what the last mand does:
npx
is a way to invoke installed npm binaries. It's effectively a package runner.tsc
is the typescript piler as a binary--outDir
is the mand line flag to indicate where to output the files.
So if your files looked like this:
foo.ts
bar.ts
After that mand, it will be:
build/
foo.js
bar.js
foo.ts
bar.ts
If you want to modify the output options, I would suggest reading the docs on tsconfig here
Option 2: Use a bundler like 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.
Since you're using Rails, I would suggest looking into something like Webpacker which will allow you to use Typescript in your Rails app
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? RubyMine, Visual Studio, etc. do support automatic TypeScript to Javascript transpiling. So you can work in TypeScript and in the background the Javascript files are automatically updated.
本文标签: Converting typescript to javascriptStack Overflow
版权声明:本文标题:Converting typescript to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318118a2452228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论