@shencom/typing
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

@shencom/typing

集成一些类型工具方法,公共接口的请求参数和响应数据类型

install

pnpm add @shencom/typing

# or

yarn add @shencom/typing

Usage

Init

配置 tsconfig

// tsconfig.json
{
  "compilerOptions": {
    "types": ["@shencom/typing"]
  }
}

Basic Usage

import { DeepMutable, Unfurl } from '@shencom/typing';

// ...

Typing

UTILS

  • Unfurl<T>: 展开对象类型

    interface User {
      name: string;
      profile: {
        address: string;
      };
    }
    type User1 = Unfurl<User>;
  • OneOf<T, U>: 类型二选一

    const A: OneOf<{ a: string }, { b: string }> = {
      a: '',
    };
    // or
    const B: OneOf<{ a: string }, { b: string }> = {
      b: '',
    };
    
    // error 不能将类型“{ a: string; b: string; }”分配给类型
    const AA: OneOf<{ a: string }, { b: string }> = {
      a: '',
      b: '',
    };
  • Mutable: 移除 readonly

    interface User {
      readonly name: string;
      readonly age: number;
    }
    
    type User1 = Mutable<User>; // type User1 = { name: string; age: number; }
  • DeepMutable: 递归移除 readonly

    interface User {
      readonly name: string;
      readonly profile: {
        readonly address: string;
      };
    }
    type User1 = DeepMutable<User>; // type User1 = { name: string; profile: { address: string } };
  • DeepParameters<T, Deep=1>: 获取第 N 层函数参数,默认第二层

    const A = (b: number) => {};
    type B = DeepParameters<typeof A>; // [b: number]
    
    const AA = (b: number) => (c: number) => {};
    type BB = DeepParameters<typeof AA>; // [c: number]
    
    const AAA = (b: number) => (c: number) => (d: number) => {};
    type BBB = DeepParameters<typeof AAA, 2>; // [d: number]
  • ReturnPromiseType<T>: 获取函数返回 Promise 泛型的值

    type A = (b: number) => Promise<boolean>;
    
    type B = ReturnPromiseType<A>; // boolean
  • Dictionary<T>: 对象添加索引类型

    const a = {};
    a.a = 1; // 类型“{}”上不存在属性“a”。
    
    const aa: Record<string, any> = {};
    aa.a = 1;

SC

  • SC.API
    • Query: 搜索类型
    • Sorts: 筛选字段
    • IndexQuery: index 接口查询参数类型
    • IndexSorts: index 接口排序参数类型
    • IndexInterface<T>: 分页接口
    • IndexBodyInterface: index 接口参数类型
    • ExportBodyInterface: export 接口参数类型
  • SC.File
    • Info: 文件信息
  • SC.Gis
    • Point: 点位信息
  • SC.OSS
    • Sign: oss 签名
  • SC.User
    • TokenRoot: 令牌信息
    • SysInfo: 系统用户信息
    • WxInfo: 微信用户信息
    • RootInfo: 登录接口返回用户信息类型
    • Info: 系统用户和微信用户并集
  • SC.CSM
    • Category: 栏目类型
    • Articles: 内容类型

第三方

  • 微信 JSSDK

Readme

Keywords

Package Sidebar

Install

npm i @shencom/typing

Weekly Downloads

0

Version

0.4.0

License

ISC

Unpacked Size

49.4 kB

Total Files

13

Last publish

Collaborators

  • shencom