首页 > 其他分享 >simpread-typescript 注释大法

simpread-typescript 注释大法

时间:2022-11-10 11:58:04浏览次数:63  
标签:大法 typescript name tag user interface simpread id string

本文由 简悦 SimpRead 转码, 原文地址 zhuanlan.zhihu.com

函数、interface、enum、type、var 的申明建议使用文档注释 (持续更新中)

api function

/**
   * 获取店铺签约合同信息
   * @access http://api.xxx.com/getUserNameByTagIdFromServer
   * @param tagId 标签id {number}
   * @returns name 用户名称 {string}
   */
  async function queryUserNameByTagId(tagId: string) {
    const userName = await getUserNameByTagIdFromServer(tagId);
    return userName;
  }

interface

interface IUser {
  /**
   * 用户姓名
   */
  name: string;
  /**
   * 用户年龄
   */
  age: number;
}

const user = {} as IUser;

user.age = 3;
user.name = '赵云';

但很遗憾的是,当我们尝试去解构 user 对象,得到的变量,它是无法推断出注释的,如下图

然后进一步研究发现,实际上是 value 部分无法自动推断,如下:

这样的遗憾同样适用于 react hooks - useState 的解构变量(因为数组实际上也是对象,它们的 keyName 是 number)

如果您找到了以上问题的解决方案,可以在评论区留言,谢谢~~

link

// types.ts
export interface ITypeA {
  /**
   * tag 标签 {@link ITag}.
   */
  tag: ITag;
}

export interface ITag {
  /** tag id */
  id: string;
  /** tag name */
  name: string;
}
// demo.tsx
import { ITypeA } from './types.ts';

const test: ITypeA = {
   tag: {
     id: '1',
     name: 'A',
   },
};

enum

/**
 * 水果枚举定义
 * @param APPLE apple 苹果
 * @param ORANGE orange 橘子
 */
enum EFruit {
  /** 苹果 */
  APPLE = 'apple',
  /** 苹果 */
  ORANGE = 'orange',
}

普通变量

标签:大法,typescript,name,tag,user,interface,simpread,id,string
From: https://www.cnblogs.com/zhuoss/p/16876573.html

相关文章