使用更具可读性的方式设置Typescript类型

TypeScript 提供了一些内置的实用类型,可以更好的方式将类型从一种形式转换到另一种形式。
这些内置的类型全局可用的,所以可以很方便的使用它们。

Typescript泛型

在了解 TypeScript 实用、类型之前,类型别名和泛型很重要。我们以在TypeScript中为任何现有类型创建类型别名。

  type MyString = string;

  let helloWorldMessage: MyString = 'Hello Wisdom Geek';

泛型用于创建可重用的类型别名。假设我们有一个identity 函数,该函数返回传递的任何值:

  const identity = (arg: string): string => arg;

如果我们要返回的是 number 类型怎么办?有小伙伴可能会用any代替特定的类型:

  const identity = (arg: any): any => arg;

但这减少了参数的类型信息,因此,也失去 TypeScript 带来的好处。我们想要以一种可以用来表示返回类型的方式来捕获参数的类型。这就是泛型派上用场的地方。我们将使用适用于类型而不是值的类型变量。

  const identity<T> = (arg: T): T => arg;

接着,在调用它时指定函数的类型:

  const output = identity<string>("Hello Wisdom Geek");

TypeScript 中的内置实用类型

  • Partial

    Pritial把 T 的所有属性变为可选。

 type BlogPost = {
   title: string;
   author: string;
 }

 type PartialBlogPost = Partial<BlogPost>;
 /* 等价于 {
   title?: string;
   author?: string;
 } *
  • Required
    Required把 T 的所有属性变为必填的。
  type PartialBlogPost = {
    title?: string;
    author?: string;
  }

  type BlogPost = Required<PartialBlogPost>;
  /* 等价于 {
    title: string;
    author: string;
  } */

  • Readonly
    Readonly把 T 的所有属性变为只读的。

      type BlogPost = {
        title: string;
        author: string;
      }
    
      type BlogPost = Readonly<PartialBlogPost>;
    
      /* 等价于 {
        readonly title: string;
        readonly author: string;
      } */
    
  • Pick
    Pick<T,K> 抽取T里的属性,属性来自K.

       type Point3D = {
        x: number,
        y: number,
        z: number,
       };
    
    
       type Point2D = Pick<Point3D, 'x'|'y'>;
       /* 等价于 {
        x: number,
        y: number
      } */
    
  • Parameters
    Parameters T 是 Function,提取函数里返回值为 tuple。

	type T0 = Parameters<() => string>;
	// type T0 = []

	type T1 = Parameters<(s: string) => void>; 
	// type T1 = [s: string]

	type T2 = Parameters<<T>(arg: T) => T>;
	// type T2 = [arg: unknown]


  • Omit
    Omit<T,K>和Pick相反(去除属性).
  type Point3D = {
    x: number,
    y: number,
    z: number,
   };


  type Point2D = Omit<Point3D, 'z'>;
  /* same as {
     x: number,
     y: number
  } */
  • Record
    Record<K,T>生成一个接口,属性为K的所有属性,k的所有属性都有T的类型
  type BlogPost = Record<'title' | 'author', string>

  /* same as {
     title: string;
     author: string;
  } */

如果所有类型都具有相同的值,则声明的 Record 版本会更加简洁和可读,因为它们都具有相同的类型。

  • Extract
    Extract<T, U> - 用于从类型T中取出可分配给U类型的成员
  type T0 = Extract<"a" | "b" | "c", "a" | "f">;
   // type T0 = "a"
  type T1 = Extract<string | number | (() => void), Function>;  
   // type T1 = () => void
  • Exclude
    Exclude<T, U> - 用于从类型T中去除不在U类型中的成员。
    type T0 = Exclude<"a" | "b" | "c", "a">;
    // type T0 = "b" | "c"

    type T1 = Exclude<string | number | (() => void), Function>;
    // type T2 = string | number
  • NonNullable
    NonNullable- 用于从类型T中去除undefined和null类型。
   type T0 = NonNullable<string | number | undefined>;
   // type T0 = string | number

   type T1 = NonNullable<string[] | null | undefined>;
   // type T1 = string[]
  • ReturnType
    ReturnType- 获取函数类型的返回类型
   type T0 = ReturnType<() => string>;
   
   type T0 = string
   type T1 = ReturnType<(s: string) => void>;
   
   type T1 = void
   type T2 = ReturnType<<T>() => T>;
   
   type T2 = unknown
   type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
   
   type T3 = number[]

   type T5 = ReturnType<any>;
   
   type T5 = any
   type T6 = ReturnType<never>;
   
   type T6 = never
   type T7 = ReturnType<string>;
  • InstanceType
    InstanceType- 获取构造函数的实例类型
   class C {
     x = 0;
     y = 0;
   }

   type T0 = InstanceType<typeof C>;
   
   type T0 = C
   type T1 = InstanceType<any>;
   
   type T1 = any
   type T2 = InstanceType<never>;
   
   type T2 = never 

参考官网

https://www.typescriptlang.org/docs/handbook/utility-types.html#uppercasestringtype

欢迎关注我的博客: https://blog.csdn.net/weixin_42323607

github地址: https://github.com/NurTuam

多多支持!本人会持续更新哒 ❤️

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值