Pick/Omit Are One Of The Best Features Of TypeScript

preview_player
Показать описание
🌎 Find Me Here:

#Shorts
Рекомендации по теме
Комментарии
Автор

Also you can use "Partial<User>"

redio
Автор

Yeh, I've wondered this. Whether to create a new type WithID<User> or make Id optional, or omit it...I'm used to the former, as for me it represents stuff from the database.

matthewbeardsley
Автор

These are incredibly useful. Thanks for sharing.

catwhisperer
Автор

Compiler doesn’t throw an error even when the target doesn’t have the omitted keys like Omit<User, 'foo'> so you should use safer way down below!

type SafeOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

mozart-
Автор

Thank you so much, and it will be so helpful continuing with videos like this by explain typescript type system using real world examples.

yosefrahahleh
Автор

This should be used with caution though. Some projects that have certain types or view models may require those fields. Which is why you would normally make them optional properties with a “?” after the property name. So picking or omitting may cause breaking changes to other places that type or view model is being used.

lucasdale
Автор

You could also make the id field optional

jjfattz
Автор

Well I use partial<> for similar purpose. But thos looks nicer

simonjanca
Автор

I work on a system that is very complex, but shouldn't be that way. Sometimes I spend more time dealing with cascading changes in types (change in one place and have to change in many other places), than actual coding.

_clei_
Автор

Keep bringing these videos .. love it.

AbhishekSingh-vldp
Автор

One of my initial gut reactions here is that this looks nice in the same way inheritance looks nice. I can imagine Pick & Omit being useful when used with say lodash's pick & omit. I can also imagine that if this stuff is used more than a single level deep (e.g. using Pick & Omit on a type that has already been Picked or Omitted) that headaches will quickly come. I know the IDE will give intellisense on this, but the dev experience of managing this mentally sounds quickly unmanageable.

baka_baca
Автор

It creates dependency chains on the original types.

TheNeel
Автор

most usefull thing i have seen all day

isi
Автор

Extract is another powerful one.

type Colors = 'red' | 'blue' | 'green';

type WithoutBlue = Extract<Colors, 'blue'>;

type ChristmasColors = WithoutBlue; // only 'red | 'green' are allowed

sburke
Автор

Thanks I’ll use it in my angular project

brigaderog
Автор

Thats nice info Kyle.
But you could have done this by adding ? in id in User itself.

jagdeeshbhavani
Автор

How do you make shorts like this? I’m planning on doing it for work and can’t figure out how.

chrisdizzle
Автор

Hi Kyle, learning next js and the typescript is driving me crazy, got any videos to help me better understand?

Veedsify
Автор

Ts for creating problems or solving problems?

joydeepbhowmik
Автор

awesome! you can reuse a type, and omit id, on a form. neat.

wandreperes