You can add this type to your get function: const get: (key: T | T[], callback: (result: Partial) => void) = ... With this you can infer the correct type for 'result' within your callback and it will only offer the data for keys you have provided .
@JoshuaMorony
Жыл бұрын
Awesome thanks, I'll look into that
@JoshuaMorony
Жыл бұрын
This was a fantastic improvement, thank you - for anyone interested in the full implementation of the example from the video with this approach this is what I ended up doing: const get = ( key: T | T[], callback: (result: Partial) => void ): void => { chrome.storage.local.get(key, (items: { [key: string]: any }) => { callback(items as Partial); }); };
@donaldmurillo4782
Жыл бұрын
The “as Type” is such a trap for new developers…
@deatho0ne587
Жыл бұрын
For the nubmer one if that is all one cares about export function typeGuard(obj: unknown): obj is T { return obj instanceof T; // can add (obj != null) checking for null and undefined // undefined just incase it is an API you do not control } const x = something; // where something can be anything if(typeGuard(x)) // is number type if(typeGuard(x)) // is string type if(typeGuard(x) // is SomeTypeYouCreate type, which can be an object an array etc... It is just a Generic Guard that I have started using recently.
@Alex-bc3xe
Жыл бұрын
I know so little about angular and typescript when I see you but I have determination of a sick mental person
@pravinmane1
7 ай бұрын
Great one, again 💯
@kjn5991
Жыл бұрын
Could've mentioned Discriminated Unions in the unions-section. I mean, some people might not know how to get the correct type when accessing one of the interfaces inside the union. In your example, you have the "action"-field in all those interfaces which would act as a "tag" or "index" for that specific interface. if(message.action == 'visit') { // type of message is MessageVisit and the specific fields of that interface would be accessible. }
@JoshuaMorony
Жыл бұрын
I wasn't aware specifically of the concept of Discriminated Unions - thanks!
@tomfrank2115
Жыл бұрын
Great video as always :) I knew 80% of the content :)
@JoshuaMorony
Жыл бұрын
Along with new content I've released, my weekly newsletter also often contains additional exclusive content. Check it out here: mobirony.ck.page/4a331b9076
@julienwickramatunga7338
Жыл бұрын
Nice tips, thanks !
@boris8983
11 ай бұрын
Question for the first example: what is the difference between the shown solution and just writing: callback: (result: Partial ) => void ...?
@StephenMoreira
Жыл бұрын
Great content, I think now a days TypeScript is the main thing in the Angular projects I look to improve my own skill set with, but I also feel like I need to be careful, there's a ton of neat features which I don't think really safe to use because it requires way too much cognitive overload for either me or the next developer that might be on the project. First tip I will for sure steal, I believe I do it myself somewhere in a much more hacky way....thanks !
@LetrixAR
Жыл бұрын
I would be cool to see an implementation of Pick and Omit for objects, instead of types.
@jordans133
Жыл бұрын
Depending on the use case, you may be able to use object destructuring and the spread operator to accomplish something equivalent.
@kresimirnurnberger6567
Жыл бұрын
Isn't `result: {[K in StorageDataKey]?: StorageData[K]}` the same as just `result: StorageData`?
@TheAUa
Жыл бұрын
Yes it is actually is in that example 🙃. However, that type would make every key in StorageData optional, but even for that you could just go with Partial
@ahmadbenchakhtir5787
Жыл бұрын
the same note, i really work hard to know the new stuff and then realize that those the same hhhhh.
@Wielorybkek
Жыл бұрын
I didn't know about the ! operator, thanks!
@stephendebeauchamp356
Жыл бұрын
is this chrome extension you're examples are from in your GitHub?
@JoshuaMorony
Жыл бұрын
Sorry no, this is from my private games monorepo
@stephendebeauchamp356
Жыл бұрын
@@JoshuaMorony can always hope :) thanks for all you do here, very much appreciated
@deadlyecho
Жыл бұрын
Sorry, why in the first example didn't you just assign the result the type storage directly, isn't it the same ?
@JoshuaMorony
Жыл бұрын
I think the example from @TheAUa would be more appropriate here: const get: (key: T | T[], callback: (result: Partial) => void)
@deadlyecho
Жыл бұрын
@@JoshuaMorony Thanks man, this is alot better, I understood now, thanks to @TheAua
@ThiagoZeroUm
Жыл бұрын
Sadly that 'uncommon type' at 0:02 always returns never. Disappointed but not surprised.
@siddharthsidh2781
Жыл бұрын
Something is wrong I knew everything in that video..
@andrewscoding9475
Жыл бұрын
Great content as always Joshua! One type i've written and have been using in Angular looks like this - Let me know your thoughts! :D // Signal Type. export type SignalType = T extends Signal ? U : never; // Component class. vm = this.service.state; submit(vm: SignalType) { // We get the this.vm inferred type as long as we pass it back to us from the template submit(vm()) event } Cheers!
Пікірлер: 29