简体   繁体   English

在 React 项目中从 Typescript 迁移

[英]Migrating away from Typescript in a React project

I've done some googling, and haven't found a solid answer.我做了一些谷歌搜索,并没有找到一个可靠的答案。 In fact, I keep finding the exact opposite answer to my question (ie, how to migrate TO TypeScript rather than from).事实上,我一直在寻找与我的问题完全相反的答案(即,如何迁移到 TypeScript 而不是从)。 As a TS newbie, I'm trying to consider the pros/cons of using it to pitch it to my org.作为一名 TS 新手,我正在尝试考虑使用它向我的组织推销它的利弊。 My main question:我的主要问题:

If we did adopt TS, what is the level of effort of removing it from a React project if we change our minds later?如果我们确实采用了 TS,如果我们稍后改变主意,将其从 React 项目中移除需要付出多大的努力?

My understanding is that TS transpiles to JS.我的理解是 TS 转译为 JS。 If we wanted to switch to JS later on, we could simply take those transpiled JS files, check them into source, and remove the TS transpiler and files - no more TS.如果我们以后想切换到 JS,我们可以简单地获取那些转译的 JS 文件,将它们签入源代码,然后删除 TS 转译器和文件——不再需要 TS。 However, React itself has its own conventions that aren't plain JS, so I believe the transpiled files will be JS rather than JSX.但是,React 本身有自己的约定,不是纯 JS,所以我相信转译的文件将是 JS 而不是 JSX。 Is there a method of transpiling React with TS to React directly that's as effortless as going directly to JS?有没有一种方法可以将 React 与 TS 直接转换为 React,就像直接转到 JS 一样轻松?

As per my comment, you can set jsx: "preserve" as an option in your TS configuration file, which will make the compiler forego transpiling the JSX into regular JS and output .jsx files you can use.根据我的评论,您可以在 TS 配置文件中将jsx: "preserve"为一个选项,这将使编译器放弃将 JSX 转换为常规 JS 并输出您可以使用的 .jsx 文件。 Happy to help!乐于帮助!

the easiest way is to create a new project最简单的方法是创建一个新项目

steps to do this.执行此操作的步骤。

run tsc delete node modules folder运行 tsc 删除节点模块文件夹

use your terminal to delete all ts files exp: rm -r / .ts or / /.tsx使用终端删除所有 ts 文件 exp: rm -r / .ts 或/ /.tsx

copy the js code to a new react project将 js 代码复制到新的 react 项目中

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

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