đ§ TypeScript Utility Types
TypeScript can be super useful. Its type checking can reduce runtime errors during development and help solve codebase scalability challenges. Personally, I love TypeScript. At least, most of the time. Occasionally I get frustrated with a failing build due to a type issue, even though the code works just fine.
Fortunately, there exists some pretty awesome Utility Types, which I wish I had learned about sooner. With these built-in types, it is easier to overcome these types (no pun intended) of issues and they work better than using any
or unknown
.
Below are a few of my favorites.
Record
Record<Keys, Type>
[1]
Not every data structure in my code has an explicit type
definition or a finite list of keys
. In these situations Record
is a nice utility that generates a dynamic type with constrains for the keys and values of an object.
// before
const flags = {};
flags.myFeature = true;
// âī¸ Property 'myFeature' does not exist on type '{}'
// after
const flags: Record<string, boolean> = {};
flags.myFeature = true;
// âī¸ valid!
Partial
Partial<Type>
[2]
Make all properties of an object optional. Super useful, especially since the alternative is usually code duplication.
// before
const updatePost = (id: number, post: Post) => { ... };
updatePost(1, { name: 'My New Name' });
// â ...is missing the following properties from type 'Post': id, content.
}
// after
const updatePost= (id: number, post: Partial<Post>) => { ... };
updatePost(1, { name: 'Twitter Post #1' });
// âī¸ valid!
Inferred
Parameters
[3] ConstructorParameters
[4] ReturnType
[5]
This is perfect for when a type definition doesn't exist or can't otherwise be imported.
// some-package/index.d.ts
type InternalOptions = ...; // not exported!
type InternalResults = ...; // not exported!
export function config(options: InternalOptions) : InternalResults;
// app.ts
import { config } from 'some-package';
type ConfigOptions = Parameters<typeof config>[0];
const myConfig: ConfigOptions = { ... };
// âī¸ valid!
type Results = ReturnType<typeof config>;
let myResult: Results;
myResult = config(myConfig);
// âī¸ valid!
Conclusion
These are the utilities that I use most frequently and they make it more convenient to work with TypeScript. I recommend reading through then entire list and seeing what built-in types could simplify your life.