Utility types let you create types based on other types. Have a look at the official TypeScript page for example about utility types.
Partial |
Constructs a type with all properties of Type set to optional. |
|---|---|
Required |
Constructs a type consisting of all properties of Type set to required. Opposite of Partial. |
Readonly |
Constructs a type with all properties of Type set to readonly |
Record |
Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type. |
Pick |
Constructs a type by picking Keys from Type |
Omit |
Constructs a type by picking all properties from Type and then removing Keys |
Exclude |
Exclude; |
// results in "b" | "c" |
|
// “a” got excluded because it is in both UnionType and ExcludedMembers |
|
Extract |
Extract; |
// results in "a" |
|
// because only “a” is in both Type and Union |
|
NonNullable |
NonNullable |
// results in string | number |
|
// because undefined and null get removed |
|
Parameters |
Parameters void>; |
// results in [s: string] |
|
// because the parameter of function Type is a string |
|
ConstructorParameters |
Constructs a tuple or array type from the types of a constructor function type |
ReturnType |
ReturnType string> |
// results in string |
|
// because function Type returns a string |
|
InstanceType |
Constructs a type consisting of the instance type of a constructor function in Type |
| `class C { | |
| x = 0; | |
| y = 0; | |
| }` | |
InstanceType |
|
// results in C |
|
ThisParameterType |
Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter |
OmitThisParameter |
Removes the this parameter from Type |
ThisType |
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type. Note that the noImplicitThis flag must be enabled to use this utility. |
Uppercase |
|
Lowercase |
|
Capitalize |
|
Uncapitalize |
To help with string manipulation around template string literals, TypeScript includes a set of types which can be used in string manipulation within the type system. |
Omit property from type
Omit allows you to create a new type based on an existing type, but specify properties of the existing type that you want to have omitted in the new type (don’t mix that up with Exclude):
Omit<{ a: string, b:string }, 'a' > === { b: string }Omit is the opposite of Pick.
Pick<{ a: string, b:string }, 'a' > === { a: string }Exclude is different from Omit, because it takes a union type and removes a constituent of that union.
Exclude<string | number, string > === numberPartial
setTextLayerProps is a function that takes props and sets them on an object. Using Partial (which is a build-in TypeScript feature) we can specify that only props of a specific type are allowed to be passed in as props. In the following example we only allow props from TextLayer.
function setTextLayerProps(project: Project, id: string, props: Partial<TextLayer>) { const layer = project.layers.find(l => l.id === id);
if(layer) { Object.entries(props).forEach(([k, v]) => { (layer as any)[k] = v; }); }}
const project: Project = { layers: [imageLayer, textLayer], size: projectSize};
setTextLayerProps(project, "10", { text: "hello", shouldNotWork: true // Good: TypeScript complains, that shouldNotWork is not part of the props of TextLayer});We can improve this example and make it more variable: We want to allow props depending on the generic type: If we define TextLayer, then only allow props from TextLayer, if we define ImageLayer, then only allow props from ImageLayer:
function setLayerProps<T extends TextLayer | ImageLayer>(project: Project, id: string, props: Partial<T>) { const layer = project.layers.find(l => l.id === id);
if(layer) { Object.entries(props).forEach(([k, v]) => { (layer as any)[k] = v; }); }}
const project: Project = { layers: [imageLayer, textLayer], size: projectSize};
setLayerProps<TextLayer>(project, "10", { text: "hello" // Good: only TextLayer props are allowed});
setLayerProps<ImageLayer>(project, "20", { src: "some image source" // Good: only ImageLayer props are allowed});
setLayerProps<ImageLayer>(project, "30", { src: "some image source", shouldNotWork: true // Good, compiler complains});