No video

Mapped Types Explained: Keep Your Types in Sync Automatically - Advanced TypeScript

  Рет қаралды 6,590

Typed Rocks

Typed Rocks

Күн бұрын

Пікірлер: 40
@whotao6259
@whotao6259 21 күн бұрын
The mapped type might be more succinct and useful if it didn’t have an “extends” in the keys and the original types were actually used. For example: type MyType = { [K in key of T as `on.${Capitalize}`]: (value: T[K]) => any } When you iterate over the keys of some generic object, TS assumes that it’s “symbol | string | number”. While these types aren’t strings, they’re still “string able”, i.e. you can use them in template strings like `prefix.${string | symbol}`. You could also “assert” that the key should be a string by using extract, e.g. Extract. This automatically “returns” never if the key isn’t a string. It’s just a bit neater than having a nested extends directive; those should probably be reserved for omitting keys based on the original value.
@Typed-Rocks
@Typed-Rocks 12 күн бұрын
Looks great
@antonodman5709
@antonodman5709 Ай бұрын
Great video! Short and interesting, very valuable knowledge. Subbed! :)
@malvoliosf
@malvoliosf Ай бұрын
Waiting on the definitive `never` video...
@Typed-Rocks
@Typed-Rocks Ай бұрын
Thank you 🙏 Working on it
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@malvoliosf its there 👍. Thanks for the suggestion
@rea_kr
@rea_kr 16 күн бұрын
와! 너무 재밌게 봤어요. 구독 완료하였습니다.
@JayTailor45
@JayTailor45 Ай бұрын
Fascinating video. I'm really looking forward to learning more.
@mrbelkabachi
@mrbelkabachi Ай бұрын
Amazing explanation.
@dennis_benjamin
@dennis_benjamin 21 күн бұрын
Great interesting video and really well explained. But also this is why I think typescript is awefull and can become extremely hard to read. In a fully typed language you would just have a class for every event inheriting from a common parent class. You would have a service which has an overloaded onEvent method for each Event and you are fine.
@TimaGixe
@TimaGixe Ай бұрын
Nice video with great explanations. By the way, what IDE do you use?
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@TimaGixe Thank you, I use IntelliJ
@Caldaron
@Caldaron 23 күн бұрын
Extract does the same as the extends and doesnt need the ternary. Makes it a bit cleaner, credits go to ChatGPT ;-)
@VonCarlsson
@VonCarlsson 21 күн бұрын
You can also do "K in keyof T & string".
@Typed-Rocks
@Typed-Rocks 21 күн бұрын
Interesting. Will check it out 👍
@jishnuviswanath
@jishnuviswanath 23 күн бұрын
Nice video. First time watching you. Thank you algorithm. PS: what's the blue concentric circle with red up arrow in your editor?
@derciojds
@derciojds 21 күн бұрын
Replying just in case someone responds
@Typed-Rocks
@Typed-Rocks 12 күн бұрын
This points to the type where this property has its type origin. When I click on it it redirects me to the type.
@LePhenixGD
@LePhenixGD Ай бұрын
Amazing!
@nadavgover6017
@nadavgover6017 20 күн бұрын
Cool! What if one of the properties Events are optional?
@Typed-Rocks
@Typed-Rocks 12 күн бұрын
Then your on type will also be optional 👍😎
@tngerik14
@tngerik14 Ай бұрын
Amazing content. I'd like to know how did you setup that " // ^? " functionality to see type information permanently in the editor. You used it in previous videos. Is it a plugin? I can't find out anything about it and it doesn't work in VSCode and IntelliJ
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@tngerik14Thank you. I‘ve created a plugin called „WiTT“ and you can download it from the Marketplace in IntelliJ. plugins.jetbrains.com/plugin/23294-witt--typescript-type-hints-like-in-the-ts-playground For VSCode there is a plugin called „vscode-twoslash-queries“ 👍 marketplace.visualstudio.com/items?itemName=Orta.vscode-twoslash-queries
@tngerik14
@tngerik14 Ай бұрын
@@Typed-Rocks Thank you! But there is probably no way it can work within Vue's script section, right?
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@tngerik14 Currently not. But I will check if I can support it. Great idea 👍
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@tngerik14 Just a quick update. I've added the possibility to use it in Vue Scripts and sent it to Jetbrains for review. Will give an update as soon as it's available. Or just install the current version and get the update with vue support automatically. :)
@tngerik14
@tngerik14 Ай бұрын
@@Typed-Rocks Terrific! I looked into the repo for the vscode extension and there is an open issue to this topic too. But seems like there are some limitations and it can't be supported right now. Unfortunately I use vscode more often but your extension is even better. Less laggy, shows more characters...
@krissh_the_dev
@krissh_the_dev 21 күн бұрын
Good video
@xyrn23
@xyrn23 Ай бұрын
nice, subbed.
@yt-sh
@yt-sh 23 күн бұрын
DRY codes are awesome
@GreggBolinger
@GreggBolinger 23 күн бұрын
Dear lord, just use an interface. This is why Typescript code bases become unreadable. It's good information, don't get me wrong. But it isn't a great example of how to apply this.
@Typed-Rocks
@Typed-Rocks 23 күн бұрын
Thanks for your comment. How would you do it? A type is basically the same as an interface. Could you provide me an example how you would do other? I would love to get ideas 😎. This is an example similar how typescript itself shows by the way. www.typescriptlang.org/docs/handbook/2/template-literal-types.html
@davidntumba2447
@davidntumba2447 19 күн бұрын
Maybe it depends on the context 🤔. Based on the example, it seems that events is more of a collection that has operators. Each operator has a name "add", "remove" and a action "onAdd" etc. Maybe it's best to make an operator interface that has a name and action method and then inject or instantiate that into the events type/class. Then we need not worry about the names 🤔 Cool example tho.
@DemiGoodUA
@DemiGoodUA 13 күн бұрын
It looks like an anti-pattern in typing. You are writing code in types. Types should describe the data, not the process of acquiring it. Use “if” and generalization in code, not in types.
@Typed-Rocks
@Typed-Rocks 13 күн бұрын
But then we cant make our code typesafe that easy. We would need to check if every „on“ method exists and there would be no way in adding things like auto completion
@DemiGoodUA
@DemiGoodUA 13 күн бұрын
@@Typed-Rocks You just need to maintain the OnEvents type manually. This can be a bit annoying, but your types will be simple and clear. Also, in your case, adding a move property to the Event will force you to implement an onEvent method, but you may not need that
@Typed-Rocks
@Typed-Rocks 13 күн бұрын
If its optional then you could make the mapped typed with a „?“. For me personally I would rather have the type generated and dont have to change types everywhere when I change the basetype. But it depends on the taste :)
Вы чего бл….🤣🤣🙏🏽🙏🏽🙏🏽
00:18
طردت النملة من المنزل😡 ماذا فعل؟🥲
00:25
Cool Tool SHORTS Arabic
Рет қаралды 10 МЛН
Happy birthday to you by Tsuriki Show
00:12
Tsuriki Show
Рет қаралды 10 МЛН
КТО ЛЮБИТ ГРИБЫ?? #shorts
00:24
Паша Осадчий
Рет қаралды 1,1 МЛН
AI isn't gonna keep improving
22:11
Theo - t3․gg
Рет қаралды 139 М.
The US has planned their move to Rust (it's wild)
16:31
Theo - t3․gg
Рет қаралды 100 М.
TypeScript Wizardry: Recursive Template Literals
14:47
Tech Talks with Simon
Рет қаралды 37 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 169 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 203 М.
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 353 М.
The Story of Next.js
12:13
uidotdev
Рет қаралды 565 М.
The Easy Way to Design Top Tier Websites
11:54
Sajid
Рет қаралды 325 М.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Josh tried coding
Рет қаралды 209 М.
Вы чего бл….🤣🤣🙏🏽🙏🏽🙏🏽
00:18