jsDoc 布道
来源:dev.to
时间:2024-10-31 17:55:10 119浏览 收藏
从现在开始,努力学习吧!本文《jsDoc 布道》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
太长了;
使用遗留代码库 - 我们中的许多人无法一次又一次地躲避 - 让我尝试使用 jsdoc 而不是 typescript。我必须揭露令人惊讶的真相!
首先让我们清理一下: jsdoc 或 ts 只是意味着在开发人员时间下(包括稍后审查、重用、在任何环境中查看该代码:git 网页、随机编辑器、chrome、firefox、. ..:devtool、vim、cat ... );
第一乐章
只需打开你的编辑器,写下适当的注释来测试 jsdoc 是否正常工作。
/** @typedef { 'js' | 'jquery' | 'ts' | 'jsdoc' } phase; */ /** * on typing ' editor will popup string list. * * @type {phase} */ const badcase = 'react'; // !!!! lint alert !!!! /** @type {phase} */ const goodcase = 'jquery';
jsdoc 与 typescript
根据我的经验,jsdoc 能够替换 ts 代码,此外两者之间存在虫洞。
最大的jsdoc功能恕我直言:这是使用标准的js注释系统,所以不要破坏任何js代码,这个代码将在js能够运行的任何地方运行。
feature | jsdoc | typescript |
---|---|---|
compiling freedom | do not need compiling the code. | mandatory to compile |
dependency freedom | can working without any dependency | at least ts is your dependency |
namespace freedom | don't interfere types and other imports names. you can use same name of component and component type in react for example. | typesript names are identical including as any other referenct |
rework freedom | don't need to change existing code, just insert a comment. | at least some part in your code need to be turn to ts |
legacy freedom | can be use even transform to typescript the project is not option. many legacy projects affected this situation. | need to push management to allow that modification |
maximalism freedom | don't need to use every where. even you can use on a new commits, and after easy to search which is the new or reworked parts. | also enough to turn build system, and just part of code to ts |
future freedom | later can easy trasnlate to ts. under the hood this is use same technic, just different way. | need to work if decide to use js instead ts |
mindset freedom | because this is a comment your know well this is don't made any type check at runtime for you as ts also. | ts is noising your code |
jsdoc 编辑经验
我可以在任何编辑器中编写jsdoc,但很少有人理解它。
节点模块体验
我还创建了一个 npm 模块:jsdoc-duck:一个 jsdoc 编码模块。这强调了如果没有 typescript,就不可能创建 jsdoc npm 模块。也许如果我花更多的时间来弄清楚 vite 构建参数,那么就可以找到正确的解决方案。但好消息是,如果不在 npm 中使用该模块,而是借用我们的代码:只需将 index.js 复制到某个位置 - 那么我们就可以节省时间来向我们的程序插入新的依赖项,并且不会发生任何事情如果模块所有者将模块转为其他东西。
jsdoc <-> typescript 之间的虫洞
好消息是 typescript 和 jsdoc 相互兼容,只是 jsdoc 使用了一些不同的语法。但是您可以在打字稿中使用 jsdoc 模块类型,也可以在 javascript / js+jsdoc 程序中使用打字稿模块类型。所以最终决定权在你手中。
沿着黄砖路走
vs 代码示例展示了如何很好地看到 jsdoc 代码中的类型,在我看来,这给您的代码带来的噪音令人惊讶地减少了。
奖金
:: vs 代码片段
"@type": { "prefix": ["ty"], "body": ["/** @type {$0} */"], "description": "jsdoc type" }, "@typedef": { "prefix": ["td"], "body": ["/** @typedef {$1} foo */"], "description": "jsdoc typedef" },
:: jsdoc-duck 代码
(在此视图中语法突出显示无助于理解类型)。但是这个简短的程序是一个很好的例子,jsdoc 也可以使用 ts 的高级功能。
import { useMemo, useReducer } from "react"; /** * @template T - Payload Type * @typedef {T extends { type: infer U, payload?: infer P } ? { type: U, payload?: P } : never} ActionType */ /** @template AM - Actions Map @typedef {{ [K in AM['type']]: K }} Labels */ /** @template AM - Actions Map @typedef {{ [T in AM["type"]]: Extractextends { payload: infer P } ? (payload: P) => void : () => void }} Quack */ /** * @template ST - State * @template AM - Actions Map * @typedef {(state: ST, action: AM) => ST} Reducer */ /** * Factory function to create a typed action map. * @template AM - Actions Map * @param {Labels } labelsObject - The keys representing action labels. * @param {function} dispatch - The dispatch function for actions. * @return {Quack } The resulting typed action map. */ export const quackFactory = (labelsObject, dispatch) => Object .keys(labelsObject) .reduce( /** * @arg {Quack } acc * @arg {keyof Labels } type * @return {Quack } */ (acc, type) => ({ ...acc, [type]: (payload) => {dispatch({ type, payload });} }), {}); /** * A factory hook to create a state and a typed dispatch functions\ * @exports useDuck * @template AM - Actions Map * @template ST - State Typer * @param {(st: ST, action: AM) => ST} reducer - The reducer function to manage the state. * @param {ST} initialState - The initial state value. * @return {[ST, Quack ]} The current state and a map of action dispatch functions. */ export const useDuck = (reducer, initialState, labels) => { const [state, dispatch] = useReducer(reducer, initialState); const quack = useMemo( () => quackFactory(labels, dispatch), [dispatch, labels] ); return ([state, quack]); };
快乐编码,挖掘而不是添加依赖
本篇关于《jsDoc 布道》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
435 收藏
-
490 收藏
-
150 收藏
-
226 收藏
-
188 收藏
-
118 收藏
-
223 收藏
-
211 收藏
-
221 收藏
-
463 收藏
-
395 收藏
-
418 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习