I still haven't done enough TS to be much more than an "armchair theoretician" (typical KZread commenter) at this point... but, yeah, Phase 3 is best! ;)
@yeorinim2sida2 күн бұрын
Based on this video, I created code that specifically changes the return type. I have the following union type and wanted to narrow down the type of the returned array depending on whether it's 'title' or 'body'. export type IndexMap = | { type: 'title'; keyword: string; target: Title } | { type: 'body'; keyword: string; target: Body } Therefore, I defined the return type as follows: function searchAt<T extends IndexMap['type']>( content: string ): Extract<IndexMap, { type: T }>['target'][] { return [] } If the type is 'title', it returns a Title[] and if it is 'body', it returns a Body[]. searchAt<'title'>('.....') returns Title[] searchAt<'body'>('....') returns Body[]
@-anonim-30082 күн бұрын
Thanks a lot! That's easy)
@bobsawey3 күн бұрын
thx
@JPilsonSumbo3 күн бұрын
People get into these problems for misusing enums, you want to do things that enums were not designed for. Enums are designed to represent a fixed set of related constants with descriptive names, which makes code more readable and maintainable.
@WendiCahyono-yf7xc3 күн бұрын
as the name suggests
@hatemtraif41644 күн бұрын
The way I read this now, this is similar to as const, but with as const you are narrowing the entire thing as a value while this will narrow the types for you only ? I saw also another comment that satisfies can be mixed with as const, what are actual use cases were combining both is desired?
@AlvaroDevLabs4 күн бұрын
Best explanation out there of the why, and you answer the most common question, should I force ESM only? It's always depending on your users. Great video.
@pulserudeus79684 күн бұрын
awesome find! thanks
@JaquaSchao4 күн бұрын
I'm member number 50,490 on 31/05/24 use this extension. Good content, good video. Thanks <3
@josevargas6866 күн бұрын
If you like a minimal amount of dependencies, look into Fresh framework from Deno. You get EVERYTHING and ZERO build steps, it's absolutely calming.
@Zufarabyan6 күн бұрын
I guess your example is similiar to the usage of enum (?). are there any other example besides that?
@eulucaspedroabreu10 күн бұрын
Was in dare need of this explanation! Thank you, Matt!
@user-nb3nf7gc7z10 күн бұрын
this isn't type safe at runtime. The server can return something you aren't expecting.
@jasonjimenez911611 күн бұрын
Nowadays, we just let chatgpt generate the generic types. Lol
@Shogoeu11 күн бұрын
Right at the end of this video, I got a suggested video "Don't use const!"
@shatzofhudson11 күн бұрын
you could also just not use typescript.....significantly easier
@sanjivgautam441612 күн бұрын
Aren't you a fucking genius?
@chamseboulabba174212 күн бұрын
Useless , complicating things , learn how teach first , understanding things dosn't mean you know how to teach it , the worst explanation ever seen
@mattpocockuk12 күн бұрын
What felt bad about my teaching style?
@otiamaino246112 күн бұрын
Weird if they learnt js this shouldn’t be a problem I mean DOM nodes are anything from a tag to a text, and HTMLElements are html tags 🤲🏾
@real-oppenheimer14 күн бұрын
Could you show a setup where the "package" uses React? I want to use React there to e.g. write reusable hooks that are then used in the apps.
@mattpocockuk14 күн бұрын
Add "jsx" to your tsconfig.json. That's it!
@real-oppenheimer14 күн бұрын
@@mattpocockuk Thanks :)
@AbstruseJoker14 күн бұрын
This man’s content is always golden. Unlike theo and prime ranting about the most random crap
@LokiDaFerret14 күн бұрын
I think "people doing database stuff" already have written try/finally blocks. And it seems a bit pointless to rewrite existing code which has no optimal performance value. So good for new/future development. Not very useful in existing projects. 2 cents.
@zilahi814 күн бұрын
Did not get to like turborepo, however, nx is just pure awesomesness!
Пікірлер
Thank you
I still haven't done enough TS to be much more than an "armchair theoretician" (typical KZread commenter) at this point... but, yeah, Phase 3 is best! ;)
Based on this video, I created code that specifically changes the return type. I have the following union type and wanted to narrow down the type of the returned array depending on whether it's 'title' or 'body'. export type IndexMap = | { type: 'title'; keyword: string; target: Title } | { type: 'body'; keyword: string; target: Body } Therefore, I defined the return type as follows: function searchAt<T extends IndexMap['type']>( content: string ): Extract<IndexMap, { type: T }>['target'][] { return [] } If the type is 'title', it returns a Title[] and if it is 'body', it returns a Body[]. searchAt<'title'>('.....') returns Title[] searchAt<'body'>('....') returns Body[]
Thanks a lot! That's easy)
thx
People get into these problems for misusing enums, you want to do things that enums were not designed for. Enums are designed to represent a fixed set of related constants with descriptive names, which makes code more readable and maintainable.
as the name suggests
The way I read this now, this is similar to as const, but with as const you are narrowing the entire thing as a value while this will narrow the types for you only ? I saw also another comment that satisfies can be mixed with as const, what are actual use cases were combining both is desired?
Best explanation out there of the why, and you answer the most common question, should I force ESM only? It's always depending on your users. Great video.
awesome find! thanks
I'm member number 50,490 on 31/05/24 use this extension. Good content, good video. Thanks <3
If you like a minimal amount of dependencies, look into Fresh framework from Deno. You get EVERYTHING and ZERO build steps, it's absolutely calming.
I guess your example is similiar to the usage of enum (?). are there any other example besides that?
Was in dare need of this explanation! Thank you, Matt!
this isn't type safe at runtime. The server can return something you aren't expecting.
Nowadays, we just let chatgpt generate the generic types. Lol
Right at the end of this video, I got a suggested video "Don't use const!"
you could also just not use typescript.....significantly easier
Aren't you a fucking genius?
Useless , complicating things , learn how teach first , understanding things dosn't mean you know how to teach it , the worst explanation ever seen
What felt bad about my teaching style?
Weird if they learnt js this shouldn’t be a problem I mean DOM nodes are anything from a tag to a text, and HTMLElements are html tags 🤲🏾
Could you show a setup where the "package" uses React? I want to use React there to e.g. write reusable hooks that are then used in the apps.
Add "jsx" to your tsconfig.json. That's it!
@@mattpocockuk Thanks :)
This man’s content is always golden. Unlike theo and prime ranting about the most random crap
I think "people doing database stuff" already have written try/finally blocks. And it seems a bit pointless to rewrite existing code which has no optimal performance value. So good for new/future development. Not very useful in existing projects. 2 cents.
Did not get to like turborepo, however, nx is just pure awesomesness!
Thanks for deleting my comment 🤡