Const type parameters are so powerful

Const type parameters are so powerful

Matt Pocock

1 год назад

46,452 Просмотров

const modifiers for type parameters are coming to TypeScript 5.0! The PR's not even merged yet - but you can already start playing with it.

Andarist's tweet: https://twitter.com/AndaristRake/status/1602701606721814528
The PR: https://github.com/microsoft/TypeScript/pull/51865

Become a TypeScript Wizard with Matt's TypeScript Course: https://www.totaltypescript.com/

Follow Matt on Twitter: https://twitter.com/mattpocockuk

Тэги:

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


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

@zeenuexe8362
@zeenuexe8362 - 23.08.2023 20:57

WOOHOO, I cant wait to bother my bossman to upgrade to version 5

Ответить
@tmahad5447
@tmahad5447 - 12.08.2023 07:15

typescript getting f**cking complex

Ответить
@leshnevskyi
@leshnevskyi - 10.03.2023 17:07

Hey Matt, thank you for a great video! I appreciate your effort. Today I just stumbled upon a use case for that feature, and I cannot wrap my head around solving it with the existing approaches. Given:

type Column = {
name: string;
}

type Template<C extends Column = Column> = {
sortBy: Template['schema'][number]['name'];
schema: C[];
}

const template: Template = {
sortBy: 'help',
schema: [{name: 'hello'}, {name: 'there'}],
}

is there any way to make it complain about the passed value to sortingColumnName and allow only 'hello' | 'there'?

Any help would be greatly appreciated!

Ответить
@TreSeymour
@TreSeymour - 09.01.2023 19:41

Ok, so I really don't want to get beat up here. But I work with TS and other langs day-to-day. What's the big benefit of this? I get the "feeling" its about "Intellisense". True | False?

Ответить
@Primordial_Bytemancer
@Primordial_Bytemancer - 23.12.2022 08:31

Extremely helpful. Thanks

Ответить
@greendesertsnow8441
@greendesertsnow8441 - 22.12.2022 08:31

Typescript is bureaucracy .

Ответить
@user-mf1ft6iz4l
@user-mf1ft6iz4l - 22.12.2022 08:27

I'm at a loss for words. great. thank you

Ответить
@risitas5874
@risitas5874 - 17.12.2022 20:48

I thought I knew typescript because I know how to use "interface"

Now I feel like I don't know it at all.

Ответить
@P3PPING
@P3PPING - 17.12.2022 04:45

It this point, I'm surprised MS hasn't just made a C# to JavaScript transpiler....

Ответить
@itytYT
@itytYT - 16.12.2022 21:48

We can still have a working "makeRoles" function in the current typescript version by sprinkling some "readonly" and "as const". So is "const T" just some syntactic sugar?

Ответить
@HSBTechYT
@HSBTechYT - 16.12.2022 19:21

Hey Matt,

I have a question

Is there any way to write a mongo query which can be typed .

I tried FilterQuery but it accepts any value which is not in the interface

Ответить
@NeilChudleigh
@NeilChudleigh - 16.12.2022 17:41

So weird, I was struggling with a LocalStorage implementation that uses Generics last night and this perfectly solves the problem. Thanks!

Ответить
@HeinekenLasse
@HeinekenLasse - 16.12.2022 05:39

Great video! New to the channel, subbed

Ответить
@DuncanKrebbers
@DuncanKrebbers - 16.12.2022 01:09

Seems like a nice improvement! Any idea why the extra 'const' keyword is necessary in the generic since you're defining the object 'as const' already?

Ответить
@AnthonyTrimble
@AnthonyTrimble - 15.12.2022 16:06

Could you not just `return T as const`

Ответить
@ColinRichardson
@ColinRichardson - 15.12.2022 15:25

Do you have a super secret version of the playground? As I can't get 'insider' only 'nightly' ?

Ответить
@lengors7327
@lengors7327 - 15.12.2022 14:39

This is so awesome

Ответить
@mateuscortianoschwarz7276
@mateuscortianoschwarz7276 - 15.12.2022 14:35

I always use Jimi Hendrix as a mock name on my drafts lol

Ответить
@jelliedfish6845
@jelliedfish6845 - 15.12.2022 11:21

finally, Narrow trick is not reliable at times. This feature is great, you can infer as const and then remap type omitting "readonly" recursively. This is same as Narrow but more stable

Ответить
@RasmusSchultz
@RasmusSchultz - 15.12.2022 10:19

Any idea why it defaults to erasing most of the inferred type information in the first place?? 🤷‍♂️

Ответить
@bhumit070
@bhumit070 - 15.12.2022 04:52

I am getting error while using this in typescritpt playground
I selected nigly build but I get error like this

function something<const T>(data: T) {
return data
}

Variable 'T' implicitly has an 'any' type.(7005)

Ответить
@tomassavigliano5056
@tomassavigliano5056 - 15.12.2022 03:40

This EXACT issue has been the bane of my existence. I cannot even begin to describe how many times I've googled something akin to "infer generic as const."

Ответить
@kadensharpin2156
@kadensharpin2156 - 15.12.2022 01:56

Before you had to write
function a<T>(...tuple: readonly [...T]) {}
const is much more clean and idiomatic

Ответить
@fifty6737
@fifty6737 - 15.12.2022 01:55

i fucking love TypeScript, playing around with the genius typing system is fun

Ответить
@ofeenee
@ofeenee - 15.12.2022 00:47

Your TypeScript course is already out?

Ответить
@tobias-edwards
@tobias-edwards - 15.12.2022 00:45

I don't like polluting TypeScript with more gobbidy goop cos it makes things less readable

Ответить
@oscarh4066
@oscarh4066 - 14.12.2022 23:57

Thanks, Matt! Just purchased your Total Typescript course today :)

Ответить
@axios7776
@axios7776 - 14.12.2022 22:44

It’s taken too long

Ответить
@brianlaw4706
@brianlaw4706 - 14.12.2022 21:53

I wish this feature were available now, would've saved me a lot of grief XD

Ответить
@michaelm8044
@michaelm8044 - 14.12.2022 21:50

Great video, thanks.

Ответить
@tedchirvasiu
@tedchirvasiu - 14.12.2022 21:34

Is there any other language with a type system more insane than Typescript's?

Ответить
@Tjkrusinski
@Tjkrusinski - 14.12.2022 20:33

Homie Matt cranking out those videos. You love to see it. Low key learn something from everyone.

Ответить
@nathanfranck5822
@nathanfranck5822 - 14.12.2022 20:28

Stuff's great. I didn't know about either technique, so making toy libraries for myself I just had to remember to <const> my own objects I pass into functions, definitely not something you'd want to force your users to do :)

Ответить
@productlog5895
@productlog5895 - 14.12.2022 19:57

amazing feature!

Ответить
@amitmalaker5445
@amitmalaker5445 - 14.12.2022 19:48

I just want one feature: i)structural pattern matching

Ответить
@whitefluffycloud
@whitefluffycloud - 14.12.2022 18:41

Great stuff as always. Thanks for the update!

Ответить
@Petoj87
@Petoj87 - 14.12.2022 18:35

This is so cool

Ответить
@user-pb2su1nb4j
@user-pb2su1nb4j - 14.12.2022 18:28

I just needed this exact feature yesterday for the first time, what a coincidence.
I wish the same thing happened to all features I felt the need for...

Ответить
@MrTrickydisco
@MrTrickydisco - 14.12.2022 18:17

I'd it possible to get that code example shown in the TS playground?

Ответить
@thematrix185
@thematrix185 - 14.12.2022 18:15

The flags when you pass admin has canCreatePost as optional and undefined, surely it shouldn't exist because it doesn't exist in the flags object for an admin?

Ответить
@lapulapucityrider3227
@lapulapucityrider3227 - 14.12.2022 18:07

Maya nimo opawa oi biliba

Ответить
@whoman7930
@whoman7930 - 14.12.2022 18:05

I can't wait for v5 to drop.

Ответить
@anhdunghisinh
@anhdunghisinh - 14.12.2022 18:02

WoW TS is more and more versatile, my only concern is will it's performance improvement would catch up with these changes thou or the server will still clanky as before

Ответить
@fus-ro-dah
@fus-ro-dah - 14.12.2022 17:40

Another TS frustration gone.
Now I just need to get rid of all the hacks from my code that previously made this possible.

Ответить
@PokeHunterStudio
@PokeHunterStudio - 14.12.2022 17:30

Niceee

Ответить
@TiredOfWebdev
@TiredOfWebdev - 14.12.2022 17:25

first

Ответить