Svelte：TypeScript 并“不值得”用来开发库
TypeScript JavaScript
![](/data/attachment/album/202312/26/234825lw793i739kft0fj9.jpg)


TypeScript 的使用率在不断上升，Svelte 的开发者 Rich Harris 解释了为什么反其道而行，从 TypeScript 切换到 JavaScript 和 JSDoc。


Svelte 的一个将 TypeScript 转为 JSDoc 的[拉取请求](https://github.com/sveltejs/svelte/pull/8569)引起了一些困惑的评论。评论中有人说：“这个改变是出于什么原因呢？我在到处寻找这个问题或相关讨论，但我没有找到。” 随后，这个问题在 GitHub 上因“讨论过于激烈”而被锁定回复。


在上个月的一次 Svelte Society [采访](https://youtu.be/MJHO6FSioPI)中，Harris 提供了进一步的背景信息，他说：“我们决定要做的一件事就是在 Svelte 核心代码库中脱离 TypeScript，转向使用 JavaScript。这里有一些细微的复杂性我未曾充分解释。”


他持有的观点是：“类型是非常好的，但是 TypeScript 确实有些困扰…… 当你开始使用 .ts 文件后，你就必须有相应的工具来支持…… 当你使用像 TypeScript 这样的非标准语言时，你会遇到很多阻碍，我已经开始认识到这并不值得。因此，我们将我们所有的类型都放入了 JSDoc 注解中，我们也能获得所有的类型安全性，但没有任何的缺点，因为它就是 JavaScript，所有的东西都在注解中，你可以直接运行代码。这就是我们在 Sveltekit 代码库中所做的，它在 Svelte 4.0 中表现得非常好，所以我们决定对 Svelte 同样采取这种方式，因为这将让我们能够更快速地前进。”


虽然 Svelte/SvelteKit 并非最受欢迎的 JavaScript 框架，但它却是广受好评的框架之一。


开发者倾向于使用 TypeScript，主要因为他们发现强类型降低了错误的发生率，并提升了编码过程中的体验，如代码自动补全和即时帮助等功能。然而，令人惊讶的是，主要做为 API 文档工具的 [JSDoc](https://github.com/jsdoc/jsdoc)，也可以进行类型检查。这项功能已直接内置在 Visual Studio Code 中，如 [这篇文档](https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript) 所述。开发者只需在 JavaScript 文件顶部加上：



```
// @ts-check

```

正如文档中的解释，“当无法推断出类型时，可以利用 JSDoc 注解进行明确说明”。这个特性实际上是由 TypeScript 提供支持，这意味着在实际环境下，TypeScript 和 JSDoc 是相辅相成的。


不过，一个易被忽视的细节是，Harris 主要是在针对库开发的上下文里关注 TypeScript。他认为切换到 JSDoc 在开发应用时，“可能收益不大”，他说道：“如果你在开发一个应用，无论怎样你都不可避免地需要一个构建步骤。你需要优化代码，需要代码压缩，需要打包各种资源。而如果你在构建一个库，我将极力推荐你使用 JSDoc。”


Harris 在 Hacker News 进一步 [补充](https://news.ycombinator.com/item?id=35892250)，“Svelte 的用户无需担心，这个变动不会影响到你与 Svelte 使用 TypeScript 的能力——从 Svelte 导出的函数仍然会有所有熟悉的 TypeScript 好处，如类型检查，智能感知，内联文档等”。他坚定地表示：“我们对 TypeScript 的承诺比以往任何时候都更为坚决。”


*（题图：DA/e20ff1ee-6388-42ce-8d82-66bc6eebf63c）*




---


via: <https://devclass.com/2023/05/11/typescript-is-not-worth-it-for-developing-libraries-says-svelte-author-as-team-switches-to-javascript-and-jsdoc/>


作者：[Tim Anderson](https://devclass.com/author/tanderson/) 译者：[ChatGPT](https://linux.cn/lctt/ChatGPT) 校对：[wxy](https://github.com/wxy)


OK
![TypeScript is ‘not worth it’ for developing libraries, says Svelte author, as team switches to JavaScript and JSDoc TypeScript is ‘not worth it’ for developing libraries, says Svelte author, as team switches to JavaScript and JSDoc](https://devclass.com/wp-content/uploads/2022/12/shutterstock_1942508920-768x511.jpg)

Svelte creator Rich Harris has made the case for switching from TypeScript to JavaScript and JSDoc, countering a trend that has seen strong growth in TypeScript usage.

A [pull request](https://github.com/sveltejs/svelte/pull/8569) for TS to JSDoc conversion attracted some puzzled comments. “Why is this change being made? I was looking around for an issue or discussion around this but I couldn’t find anything,” said one, before the issue was locked on GitHub as “too heated.”

There is further background in this [Svelte Society interview](https://youtu.be/MJHO6FSioPI) last month where Harris said: “One of the things that we’ve decided to do is migrate away from TypeScript in the Svelte core codebase, and use JavaScript instead. There’s some nuance there that I failed to adequately communicate.”

“My position is, types are fantastic, TypeScript is a bit of a pain … as soon as you use a .ts file, then you have to have the tooling to support that … there’s all of these points of friction when you use a non-standard language like TypeScript that I have come to believe makes it not worth it. So instead, we have put all our types in JSDoc annotations, and we get all of the type safety, but none of the drawbacks, because it is just JavaScript, everything is in comments, you can just run the code. This is what we do in the Sveltekit codebase and it has worked out fantastically so for Svelte 4.0, we’re going to do the same for Svelte because it’s going to enable us to move much more quickly.”

Svelte/SvelteKit is not the most popular JavaScript framework but it is one of the most admired.

Developers have embraced TypeScript because they find that strong typing reduces errors, as well as improving the developer experience in code editors with features like code completion and pop-help. However, [JSDoc](https://github.com/jsdoc/jsdoc), which is primarily an API documentation tool, can also be used for type checking. Support for this is built into Visual Studio Code as documented [here](https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript). Developers simply add:

`// @ts-check`


to the top of a JavaScript file. “When types cannot be inferred, they can be specified using JSDoc comments,” the documentation explains. This feature is actually powered by TypeScript, meaning that in reality TypeScript and JSDoc are working side by side.

One of the nuances that can easily be missed, though, is that Harris is mainly concerned about TypeScript in the context of library development. The switch to JSDoc is “less beneficial if you’re building an app because if you’re building an app, you’re going to have a build step anyway. You want to optimize your code, you want to minify it, you want to bundle everything up. If you’re building a library that is when I really strongly urge to use JSDoc instead.”

Harris [added on Hacker News](https://news.ycombinator.com/item?id=35892250) that “as a user of Svelte, this won’t affect your ability to use TypeScript with Svelte at all — functions exported from Svelte will still have all the same benefits of TypeScript that you’re used to (typechecking, intellisense, inline documentation etc). Our commitment to TypeScript is stronger than ever.”

[Ruby on Rails 8.0 first beta release: A big bet on SQLite in production ](https://devclass.com/2024/10/09/ruby-on-rails-8-0-first-beta-release-a-big-bet-on-sqlite-in-production/)

[OpenAI previews major update to ChatGPT visual interface for writing and coding](https://devclass.com/2024/10/07/openai-previews-major-update-to-chatgpt-visual-interface-for-writing-and-coding/)

[Python 3.13 delayed by 'drastic change' removal of incremental garbage collector](https://devclass.com/2024/10/04/python-3-13-delayed-by-drastic-change-removal-of-incremental-garbage-collector/)

[Why Windows 11 24H2 is a big yawn for developers](https://devclass.com/2024/10/03/why-windows-11-24h2-is-a-big-yawn-for-developers/)

[The SQLite team is preparing an efficient remote replication tool](https://devclass.com/2024/10/02/the-sqlite-team-is-preparing-an-efficient-remote-replication-tool/)

[Harness previews Cloud Development Environments, AI assistants, turns to Google for AI DevOps](https://devclass.com/2024/09/27/harness-previews-cloud-development-environments-and-ai-assistants-turns-to-google-for-ai-devops/)

[Survey: Ruby on Rails devs happy to go their own way, now prefer Stimulus.js over React](https://devclass.com/2024/09/25/survey-ruby-on-rails-devs-happy-to-go-their-own-way-now-prefer-stimulus-js-over-react/)

[GitHub adds Microsoft Azure-based EU data residency to Enterprise Cloud](https://devclass.com/2024/09/25/github-adds-microsoft-azure-based-eu-data-residency-to-enterprise-cloud/)

[Deno version 2.0 is nearly done – but after over 4 years, the project's big bets have yet to pay off](https://devclass.com/2024/09/23/deno-version-2-0-is-nearly-done-but-after-over-4-years-the-projects-big-bets-have-yet-to-pay-off/)

[Pulumi releases security management product, touts benefits of IaC using general-purpose programming...](https://devclass.com/2024/09/20/pulumi-releases-security-management-product-touts-benefits-of-iac-using-general-purpose-programming-languages/)

[Apple releases Swift 6 with improved concurrency, testing, cross-platform support – enough to lift i...](https://devclass.com/2024/09/19/apple-releases-swift-6-with-improved-concurrency-testing-cross-platform-support-enough-to-lift-it-out-of-its-niche/)

[RustConf speakers affirm Rust for Linux project despite challenges of unstable Rust, maintainer resi...](https://devclass.com/2024/09/18/rustconf-speakers-affirm-rust-for-linux-project-despite-challenges-of-unstable-rust-maintainer-resignation/)