日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TypeScript 工具类型 - Utility Types

發布時間:2023/12/19 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TypeScript 工具类型 - Utility Types 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

官方鏈接

Partial

構造一個 Type 的所有屬性都設置為 optional 的類型。 此實用程序將返回表示給定類型的所有子集的類型。

例子:

interface Todo {title: string;description: string; }function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {return { ...todo, ...fieldsToUpdate }; }const todo1 = {title: "organize desk",description: "clear clutter", };const todo2 = updateTodo(todo1, {description: "throw out trash", });

測試結果:第一個輸入參數的 description 字段被第二個參數的同名字段覆蓋了。

如果把兩個操作數前的 … 都去掉,則失去了兩個對象做交集的效果,變成了求并集。

Required 的效果和 Optional 正好相反:

interface Props {a?: number;b?: string; }const obj: Props = { a: 5 };const obj2: Required<Props> = { a: 5 }; Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.

Readonly

interface Todo {title: string; }const todo: Readonly<Todo> = {title: "Delete inactive users", };todo.title = "Hello"; Cannot assign to 'title' because it is a read-only property.

Record<Keys,Type>

構造一個對象類型,其屬性鍵為 Keys,屬性值為 Type。 此實用程序可用于將一種類型的屬性映射到另一種類型。

例子:

interface CatInfo {age: number;breed: string; }type CatName = "miffy" | "boris" | "mordred";const cats: Record<CatName, CatInfo> = {miffy: { age: 10, breed: "Persian" },boris: { age: 5, breed: "Maine Coon" },mordred: { age: 16, breed: "British Shorthair" }, };cats.boris;const cats: Record<CatName, CatInfo>

Pick<Type, Keys>

通過從 Type 中選取一組屬性鍵(字符串文字或字符串文字的并集)來構造一個類型。

例子:

interface Todo {title: string;description: string;completed: boolean; }type TodoPreview = Pick<Todo, "title" | "completed">;const todo: TodoPreview = {title: "Clean room",completed: false, };todo;

新的類型,是從 Todo 類型中把 title 和 completed 屬性摘出來。

Omit<Type, Keys>

通過從 Type 中選取所有屬性然后刪除鍵(字符串文字或字符串文字的并集)來構造一個類型。

同 Pick 相反。

interface Todo {title: string;description: string;completed: boolean;createdAt: number; }type TodoPreview = Omit<Todo, "description">;const todo: TodoPreview = {title: "Clean room",completed: false,createdAt: 1615544252770, };todo;const todo: TodoPreviewtype TodoInfo = Omit<Todo, "completed" | "createdAt">;const todoInfo: TodoInfo = {title: "Pick up kids",description: "Kindergarten closes at 5pm", };todoInfo;const todoInfo: TodoInfo

Exclude<Type, ExcludedUnion>

通過從 Type 中排除所有可分配給 ExcludedUnion 的聯合成員來構造一個類型。

例子:

type T0 = Exclude<"a" | "b" | "c", "a">;type T0 = "b" | "c" type T1 = Exclude<"a" | "b" | "c", "a" | "b">;type T1 = "c" type T2 = Exclude<string | number | (() => void), Function>;type T2 = string | number

Extract<Type, Union>

通過從 Type 中提取可分配給 Union 的所有聯合成員來構造一個類型。取交集。

type T0 = Extract<"a" | "b" | "c", "a" | "f">;type T0 = "a" type T1 = Extract<string | number | (() => void), Function>;type T1 = () => void

NonNullable

通過從中排除 null 和 undefined 來構造一個類型。

type T0 = NonNullable<string | number | undefined>;type T0 = string | number type T1 = NonNullable<string[] | null | undefined>;type T1 = string[]

Parameters

根據函數類型 Type 的參數中使用的類型構造元組類型。

例子:

declare function f1(arg: { a: number; b: string }): void;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] type T3 = Parameters<typeof f1>;type T3 = [arg: {a: number;b: string; }] type T4 = Parameters<any>;type T4 = unknown[] type T5 = Parameters<never>;type T5 = never type T6 = Parameters<string>; Type 'string' does not satisfy the constraint '(...args: any) => any'.type T6 = never type T7 = Parameters<Function>; Type 'Function' does not satisfy the constraint '(...args: any) => any'.Type 'Function' provides no match for the signature '(...args: any): any'.type T7 = never

ConstructorParameters

從構造函數類型的類型構造元組或數組類型。 它生成一個包含所有參數類型的元組類型(或者如果 Type 不是函數,則類型 never )。

type T0 = ConstructorParameters<ErrorConstructor>;type T0 = [message?: string] type T1 = ConstructorParameters<FunctionConstructor>;type T1 = string[] type T2 = ConstructorParameters<RegExpConstructor>;type T2 = [pattern: string | RegExp, flags?: string] type T3 = ConstructorParameters<any>;type T3 = unknown[]type T4 = ConstructorParameters<Function>; Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.Type 'Function' provides no match for the signature 'new (...args: any): any'.type T4 = never

ReturnType

構造一個由函數 Type 的返回類型組成的類型。

declare function f1(): { a: number; b: string };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 T4 = ReturnType<typeof f1>;type T4 = {a: number;b: string; } type T5 = ReturnType<any>;type T5 = any type T6 = ReturnType<never>;type T6 = never type T7 = ReturnType<string>; Type 'string' does not satisfy the constraint '(...args: any) => any'.type T7 = any type T8 = ReturnType<Function>; Type 'Function' does not satisfy the constraint '(...args: any) => any'.Type 'Function' provides no match for the signature '(...args: any): any'.type T8 = any

InstanceType

構造一個由 Type 中構造函數的實例類型組成的類型。

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 type T3 = InstanceType<string>; Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.type T3 = any type T4 = InstanceType<Function>; Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.Type 'Function' provides no match for the signature 'new (...args: any): any'.type T4 = any

更多Jerry的原創文章,盡在:“汪子熙”:

總結

以上是生活随笔為你收集整理的TypeScript 工具类型 - Utility Types的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。