This TypeScript Trick Blew my Mind

This TypeScript Trick Blew my Mind

Josh tried coding

7 месяцев назад

36,580 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@XxZeldaxXXxLinkxX
@XxZeldaxXXxLinkxX - 05.01.2024 10:22

Yo dog I heard you liked types in your scripts, so I put scripts in your types so you can script your types while you type your scripts

Ответить
@robwatson826
@robwatson826 - 04.01.2024 17:50

The more you know. Typescript has some amazing features. Thanks for sharing!

Ответить
@josemfcheo
@josemfcheo - 02.01.2024 01:10

A subscriber earned here

Ответить
@neoish
@neoish - 30.12.2023 12:41

Thanks quite useful but also This should be a short.

Ответить
@CrankyBarbar1an
@CrankyBarbar1an - 30.12.2023 04:32

OH. MY. GOD. IT HONESTLY BLEW MY MIND TOO.

Ответить
@albert21994
@albert21994 - 30.12.2023 03:47

introducing this to my typescript brain is like adding letters to math in 5th grade

Ответить
@edhahaz
@edhahaz - 29.12.2023 23:04

I love javascript! This is not possible in any other language, I wonder why

Ответить
@salihyil2
@salihyil2 - 29.12.2023 21:47

Where did you see this code?

Ответить
@rikschaaf
@rikschaaf - 29.12.2023 06:11

Or you could use enums.

Ответить
@filipelinharesplus
@filipelinharesplus - 29.12.2023 05:28

omfg

Ответить
@joe-skeen
@joe-skeen - 29.12.2023 03:04

Having used TypeScript from day 1 of its release and considering myself an "advanced" user, I still found myself with multiple mind-blown moments watching this

Ответить
@liberalwrecker949
@liberalwrecker949 - 28.12.2023 21:57

Why not use an object as a map and get more flexibility?

Const colors = {
red: {
500: “text-red-500”,
400: “text-red-400”
}
} as const

Type Color = colors[string]
Type Darkness = colors[string][number]

Function findColor(color: Color, darkness: Darkness) {
return colors[color][darkness]
}

Ответить
@sefatanam
@sefatanam - 28.12.2023 21:21

awesome 😮😮😮

Ответить
@asdfasdfuhf
@asdfasdfuhf - 27.12.2023 22:39

Okay, this actually blew my mind, I didn't know you could do this with typescript.

Ответить
@ARKGAMING
@ARKGAMING - 27.12.2023 22:00

This is a really cool feature

Ответить
@scottz6830
@scottz6830 - 27.12.2023 18:52

To add some more flexibility, you could infer like this `${string}-${infer R}-${string}`. that way any dashed dashed styled string would extract the color

Ответить
@OllyWood688
@OllyWood688 - 27.12.2023 11:35

Hahaha so this can cause dev/compile time errors when matching on a dynamic source and that source changes? Awesome, I'm absolutely going to annoy my colleagues with that :D

Ответить
@reed6514
@reed6514 - 27.12.2023 05:13

How's the runtime performance as the list of colors grows? How's it compare to regex?

Ответить
@mnik0128
@mnik0128 - 26.12.2023 10:41

Imaginary problems. Please stop messing editor speed with complex types that not solve any real problems

Ответить
@SonAyoD
@SonAyoD - 21.12.2023 12:01

Great tip

Ответить
@yohendryy
@yohendryy - 21.12.2023 11:24

FOR FUCK SAKE JUST USE A MAP

Ответить
@TekExplorer
@TekExplorer - 20.12.2023 05:14

this is the weirdest type system ive ever seen

Ответить
@Mayeru
@Mayeru - 20.12.2023 01:43

today i learned that Typescript needs to implement enums.

Ответить
@avneet12284
@avneet12284 - 19.12.2023 18:01

So good

Ответить
@O_Eduardo
@O_Eduardo - 18.12.2023 19:03

Nice to know about the wizardry.... but for everyone seeing this, never, ever put that on professional code base...

Typescript is creating more issues than it is solving.

Code is not meant to be fun, its about maintanability..

Ответить
@genechristiansomoza4931
@genechristiansomoza4931 - 18.12.2023 10:43

Isn't that easy in javascript. Throw error if string is not in array.

Ответить
@Irakli008
@Irakli008 - 18.12.2023 04:30

Fun and neat trick, but this is some typescript acrobatics. I would question how sensible it is to do this in a professional environment, with others who might have to work with this code.

Ответить
@InMemoryOfNeo
@InMemoryOfNeo - 17.12.2023 15:26

when i watch your videos my brain is blowing

Ответить
@codewithguillaume
@codewithguillaume - 16.12.2023 17:00

Josh: you are my favorite channel for coding.

Ответить
@tiltMOD
@tiltMOD - 16.12.2023 15:30

I wonder what's the problem with TS design/documentation that even more seasoned developers from time to time just "find out" new things about it. It's a trend that I haven't seen in other languages/libraries nearly as often as in TS.

Ответить
@IncidentlyHere
@IncidentlyHere - 16.12.2023 14:43

Though it depends on what you want to achieve, but in this case I think it makes more sense to have 'never' on the right hand side of the ternary:

type ExtractColors<S extends string> = S extends `text-${infer R}-500` ? R: never

This way it really 'extracts' color names from strings that follow the specified format, in your version it would include any malformed string as is

Ответить
@GeneralDeD9963
@GeneralDeD9963 - 16.12.2023 13:14

Thank you Josh

Ответить
@user-yx4rj5pf9j
@user-yx4rj5pf9j - 15.12.2023 19:18

my brain hurts 🫨

Ответить
@holakonoob
@holakonoob - 15.12.2023 15:53

what about if its -700 or -400 ...

Ответить
@CodeZakk
@CodeZakk - 15.12.2023 14:13

hi thanks for this video. i have one question why don't we use redux with nextjs. i have never seen a tutorial which uses nextjs with redux can you explain to me this thanks!!!

Ответить
@helleye311
@helleye311 - 15.12.2023 12:55

I really wish this was as easy in actual code not in typescript.
For this you'd basically need to text.split("-")[1], but for more complicated things the only way is writing regex with custom named capture groups, and then destructuring that. But then typescript complains that the match could be null or some groups could not get matched, and for numbers you need to parse them manually later, it's a huge mess.

basically, give me reverse string literals where you just go text.extract(`text-${const color}-500`); and you get color out (or undefined/null/empty string if it didn't match). Trademark pending.

Ответить
@codeAbinash
@codeAbinash - 15.12.2023 10:20

Typescript is a Joke and I Love it 😅.

Ответить
@froxx93
@froxx93 - 15.12.2023 07:02

I had no idea about this. Amazing. Thanks!

Ответить
@TejasThombare-rl4du
@TejasThombare-rl4du - 15.12.2023 04:31

Waiting for new advanced

Ответить
@AntNZ
@AntNZ - 15.12.2023 02:42

Omg this just save me. I having been going in circles trying to figure out how to extract and validate string values in nextjs search params

Ответить
@francoandre307
@francoandre307 - 15.12.2023 02:41

i need learn more typescript, this video blow my mind

Ответить
@joelapablaza7722
@joelapablaza7722 - 15.12.2023 02:07

I think this is already too much. TypeScript is inspired by C# to make the work more robust yet agile. And this is already too much... it doesn't make sense

Ответить
@FranFiori94
@FranFiori94 - 15.12.2023 02:06

What is this example lmao

Ответить
@ReuelTeodoro
@ReuelTeodoro - 15.12.2023 00:59

I’m confused why you added ‘number’ to type of colors

Ответить
@venicebeachsurfer
@venicebeachsurfer - 14.12.2023 23:47

You glossed over too quickly.. .what does "[number]" explain here? Like, what/how does that work...

Ответить
@27sosite73
@27sosite73 - 14.12.2023 23:07

will ai replace me today or tomorrow?

Ответить
@paulbruno4953
@paulbruno4953 - 14.12.2023 23:02

Eloquent solution, good job!

Ответить