DEV Community 👩‍💻👨‍💻

Discussion on: TypeScript utility types: Pick and Omit

Collapse
 
lexlohr profile image
Alex Lohr

Like with Partial, it helps me to have a look at the working of these types, you can find them at lib.es5.dt.s:

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};
type Exclude<T, U> = T extends U ? never : T;
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
Enter fullscreen mode Exit fullscreen mode

With Pick, a new object type is created where the keys are defined as a subset of the keys of the initial object.

Omit essentially does the same, but uses Exclude to invert the key selection.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Yep, actually have some "deeper" articles coming on generics and how to convert these into re-usable utility types 🤩

Get so excited about these things.

Collapse
 
peerreynders profile image
peerreynders

I don't disagree, however people using utility types may not be familiar with some of the more advanced TypeScript features which may be difficult to locate in the official documentation if you don't know what you are looking for:

Looking at how utility types are implemented is probably a great way to become familiar with the more advanced features.

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

Yep.

That's why I'm starting with the built-in utility types first.
Have some articles coming up about generics and the mapped types for instance.

Thanks for adding this extra information 🙌

Collapse
 
jamesthomson profile image
James Thomson

TypeScript features which may be difficult to locate in the official documentation if you don't know what you are looking for

I've recently picked up TS again after a year hiatus and this is really the most difficult part; trying to solve a more complex typing, but not having any idea what to look for. It also doesn't help that I don't find the official TS docs very intuitive.

So, does anyone have any recommendations on good learning resources for TS? More so on advanced concepts.

Thread Thread
 
peerreynders profile image
peerreynders • Edited on

Strangely enough I think I picked up the most useful information by trying to read Preact's index.d.ts and internal.d.ts which are strictly types and then back-filling with the official documentation to discover "what did I just read there?" - failing that searching for a quick article online.

That said comlink is still giving me headaches.

One key revelation that helped me a bit was that the advanced features are less about declaring types as one might be used to from mainstream languages with a nominal type system but are more about extracting types - and that JavaScript exists in Value Space while TypeScript types exist in Type Space (which unfortunately TS syntax often conflates).

Thread Thread
 
jamesthomson profile image
James Thomson

Thanks. I also dig through other projects typings, Preact's is a good tip. It's much cleaner than some of the typings I've seen out there 😬

That Gitbook you've referenced to looks very useful, thanks for that!

Thread Thread
 
captainyossarian profile image
yossarian

If you are interested in using typescript mapped types, you can check my blog catchts.com . All examples are taken from stackoverflow

Thread Thread
 
peerreynders profile image
peerreynders

At times searching for information online can be a chore if you don't know the right terminology - terms like Non-null assertion operator or Assertion Function don't exactly suggest themselves when you are reading code.

A Glossary of TypeScript attempts to sort out some of the type related terminology.

Thread Thread
 
jamesthomson profile image
James Thomson

Very nice. That glossary is excellent 👌