简体   繁体   English

使用 React 和 .tsx 在项目中制作所有 TypeScript 的缺点

[英]Disadvantages of making all TypeScript in a project using React with .tsx

I was writing TypeScript + React (JSX) code, but sometimes I accidentally wrote JSX with the extension of .ts and got into trouble.我正在编写 TypeScript + React (JSX) 代码,但有时我不小心编写了扩展名为.ts JSX 并遇到了麻烦。 Of course, if the extension is set to .tsx , the solution will be resolved, but is there any inconvenience if all files are set to .tsx from the beginning regardless of the presence or absence of JSX?当然,如果将扩展名设置为.tsx ,解决方案就解决了,但是如果所有文件从一开始就设置为.tsx ,不管有没有 JSX 有什么不便吗?

If all files are made with .tsx,如果所有文件都是用.tsx,

" .ts has an extension conflict with MPEG-2 TS, but .tsx does not." .ts与 MPEG-2 TS 有扩展冲突,但.tsx没有。”

"Even if you need JSX later, you can write as it is, no need to change the file name." “即使以后需要JSX,也可以照原样写,不需要改文件名。”

Advantages such as can be expected.诸如此类的优点是可以预期的。

I know that我知道

Cannot cast format.无法转换格式。

but besides that I hope you can tell me if there is something like that.但除此之外,我希望你能告诉我是否有类似的东西。

A .tsx file has such inconveniences. .tsx文件有这样的不便。

What I checked我检查的

  • Allow JSX without using .tsx extension - Conversely, suggest that you can write JSX in .ts (closed)允许 JSX 不使用.tsx扩展名- 相反,建议您可以在 .ts 中编写 JSX(关闭)

  • There are two types of cast notation: value as type and <type> value , and the latter cannot be written in a .tsx file有两种类型的转换符号: value as type<type> value ,后者不能写入.tsx文件

Environment环境

  • TypeScript 3.7.5打字稿 3.7.5
  • React 16.12反应 16.12
  • Webpack 4.41.6 Use ts-loader to load to Webpack (type check is thrown to fork-ts-checker-webpack-plugin ) Webpack 4.41.6 使用ts-loader加载到 Webpack(类型检查被抛出到fork-ts-checker-webpack-plugin
  • Editor uses Visual Studio Code编辑器使用 Visual Studio Code

There is no significant drawback to using .tsx everywhere.在任何地方使用.tsx没有明显的缺点。

The only thing you can't do in .tsx files is a small syntax difference..tsx文件中你唯一不能做的就是一个小的语法差异。

You can't use <foo> to cast.您不能使用<foo>进行转换。

For example, instead of:例如,而不是:

<any>foo

you must write:你必须写:

 foo as any

If you use the first syntax in a .tsx file it will be obvious and easy to correct.如果您在.tsx文件中使用第一个语法,它将很明显且易于更正。

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

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