Utility types
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A utility type is a generic type built into TypeScript that makes a new type from one you already have. Follow one User interface through Partial, Readonly, Pick and Omit, watch tsc reject a key User does not have, and open up Partial to find the mapped type inside.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What is Partial<User>?
Partial marks every field optional with ?, so Partial<User> is { id?: number; name?: string; email?: string }.
02Which type is { name: string; email: string }, given interface User { id: number; name: string; email: string }?
Omit keeps every field except the keys you name, so Omit<User, "id"> drops id. Pick<User, "name" | "email"> gives the same type.
03Why does Pick<User, "age"> fail?
Pick is declared as Pick<T, K extends keyof T>. keyof User is "id" | "name" | "email", so tsc reports error TS2344: Type '"age"' does not satisfy the constraint 'keyof User'.
04What happens when code assigns locked.name = "Bo" and locked has type Readonly<User>?
Readonly marks every field readonly, so tsc reports error TS2540: Cannot assign to 'name' because it is a read-only property. It is a compile-time check only.
05Which mapped type is the same as Partial<User>?
TypeScript defines Partial<T> as { [P in keyof T]?: T[P] }: P visits each key of T, and the ? makes that field optional.
المسرد
- Utility type
- A generic type that ships with TypeScript, such as Partial or Pick, that builds a new type from an existing one.
- Generic type
- A type that takes other types as arguments in angle brackets, like Partial<User>.
- Optional property
- A field marked with ? that a value may leave out, as in id?: number.
- readonly
- A modifier that lets code read a field but makes assigning to it a compile error.
- keyof
- An operator that gives the union of a type's key names: keyof User is "id" | "name" | "email".
- Union type
- A type that is one of several, written with |, such as "id" | "name".
- Mapped type
- A type built by going over keys, { [P in keyof T]: ... }, to make one field per key.
- Constraint
- A rule on a type argument, written with extends: in Pick<T, K extends keyof T>, K must be keys of T.
الملاحظات والمصادر
- Partial, Readonly, Pick and Omit are declared in lib.es5.d.ts, the type library tsc loads by default. They exist only while checking: like every type, they are erased and add nothing to the JavaScript.
- Omit does not check its keys: it is declared as Omit<T, K extends keyof any>, so Omit<User, "age"> is allowed and simply removes nothing. Pick requires K extends keyof T, which is why the typo is caught.
- readonly is checked only by tsc. At run time the object can still be changed by code that is not type-checked; Object.freeze is the run-time lock.
- There are many more utility types, such as Required (the opposite of Partial), Record, ReturnType and NonNullable; the TypeScript Handbook lists them all.
- TypeScript Handbook: Utility Types
- TypeScript Handbook: Mapped Types
- TypeScript Handbook: Keyof Type Operator
- TypeScript Handbook: Generics (generic constraints)
- TypeScript Handbook: Object Types (optional and readonly properties)
- TypeScript source: lib.es5.d.ts (the definitions of Partial, Readonly, Pick, Omit)
- MDN: Object.freeze()
اختبار يتاح بعد الخطوة الأخيرة.