Most TS devs don't understand 'satisfies'

  Рет қаралды 56,389

Matt Pocock

Matt Pocock

Күн бұрын

Пікірлер: 137
@nazaka9904
@nazaka9904 9 ай бұрын
I wish you added that 'satisfies' can be combined with 'as const'. It is obvious, but still nice to mention i think
@Niksorus
@Niksorus 8 ай бұрын
It's not that obvious, it was only recently allowed 😂 It should have worked from the start though, clearly.
@Mitsunee_
@Mitsunee_ 8 ай бұрын
afaik it basically already does that, other than the part where `as const` also makes stuff readonly, which usually just adds additional confusion
@NikiHerl
@NikiHerl 8 ай бұрын
What does the combination do?
@nazaka9904
@nazaka9904 8 ай бұрын
@@NikiHerl there are some situations where you want to infer the type as closely as you can, while ensuring the object you create follows some other type. That is where i use it
@liornz462
@liornz462 9 ай бұрын
You have made such an impact on the TS community, and enriched us all with your courses, articles and KZbin content. Thank you so much!! I wish us all a wonderful new TypeScript year 🎉
@bintang9080
@bintang9080 8 ай бұрын
clear
@n4bb12
@n4bb12 9 ай бұрын
Before "satisfies" you had to make a choice whether you want something to adhere to a type OR be of the more specific type. With "satisfies" you get both.
@QwDragon
@QwDragon 9 ай бұрын
You could've done both, but in a very odd manner.
@ibgib
@ibgib 8 ай бұрын
`satisfies Partial` phenomenally useful
@JacobDrebin
@JacobDrebin 5 ай бұрын
Thanks for this explanation! In essence, this is like writing: const name: extends string = "sam"; Which is something I've always wanted to be able to do.
@cool_scatter
@cool_scatter 9 ай бұрын
I would have loved you to show off satisfies's power a little more at the end by showing one of those massive config objects! It can get really useful when combined with discriminated unions for different types of config options as well
@edgeeffect
@edgeeffect 9 ай бұрын
Seems like ages since you last popped up in my feed and said "what's up wizards".... welcome back. :)
@EngineerNick
@EngineerNick Ай бұрын
oh man thankyou I have been looking for this functionality/keyword for like an hour now. So hard to search for if you dont know what it is called. knew I would find it here! I was so sure that the `as` keyword was not the end of the line.
@TheedonCritic
@TheedonCritic 9 ай бұрын
You're a champ, Matt. Thanks!
@laminecherif7724
@laminecherif7724 8 ай бұрын
The best explanation regarding satisties I have encountered so far!
@BoudewijnDanser
@BoudewijnDanser 8 ай бұрын
All the best for 2024 Matt, hope you keep making awesome content for all the wizards!
@locotes
@locotes 4 ай бұрын
Brilliant explanation, thanks!
@Kuwaaito
@Kuwaaito 9 ай бұрын
Being unfamiliar with being able to satisfy seems to be a common issue with programmers.
@dog4ik
@dog4ik 9 ай бұрын
primeagen have 4 kinds lol
@xbsidesx
@xbsidesx 8 ай бұрын
I started using satisfies more because of its “const” value as well. This is such a good tip. Awesome video as always!
@budivoogt491
@budivoogt491 5 ай бұрын
Great explanation Matt! Thanks. Will try to narrow my types further where possible to get more autocompletion.
@SudeeptoDutta
@SudeeptoDutta 9 ай бұрын
Thank you for this clear explanation. Please continue making such videos focusing on a specific topic only.
@sarimsarimsarim
@sarimsarimsarim 9 ай бұрын
Can you describe how satisfies is different from "as const" which also seems to narrow the type. Thanks and happy new year :)
@mattpocockuk
@mattpocockuk 9 ай бұрын
as const makes a value deeply readonly satisfies checks that a value satisfies a type
@WaltersWatching
@WaltersWatching 9 ай бұрын
And sometimes it's nice to use both at once.
@snake1625b
@snake1625b 8 ай бұрын
So if you know that your objects field are never going to be mutated then use as const. If you're adding new fields then use colon. If your mutating existing fields then use satisfies?
@mikael.sevigny
@mikael.sevigny 8 ай бұрын
Fantastic content once more, thanks Matt! I appreciate the single-topic video format as well. I'm also looking forward to the book release, especially since the premium TT courses are a bit too expensive for me to purchase at the moment.
@alexjohnson-bassworship3150
@alexjohnson-bassworship3150 9 ай бұрын
I've been waiting for more satisfies videos!!
@Dev-Siri
@Dev-Siri 9 ай бұрын
typing a word like "satisfies" is satisfying
@Matiast9477
@Matiast9477 9 ай бұрын
I’m trying to step up my TS game and your videos are really helping me!
@tonimaunde
@tonimaunde 9 ай бұрын
Happy new year, Matt. Thanks a lot for your work, and I have to say that satifies still eludes me, but this video provided a nice use case.
@ExponentialWorkload
@ExponentialWorkload 8 ай бұрын
instead of the variable annotation in the first example, it is possible to also just use `as` - although this can cause other issues
@re.liable
@re.liable 9 ай бұрын
Happy holidays Matt
@budi6881
@budi6881 8 ай бұрын
hi Matt, totally love ur content! im curious how did you had those `// ^? (property) ...`?
@ContortionistIX
@ContortionistIX 8 ай бұрын
i needed this, thank u brother
@Ked_gaming
@Ked_gaming 8 ай бұрын
I wish this wasn't a totally different syntax from variable annotation, this is going to confuse the hell of out people not used to typescript
@kunodragon4355
@kunodragon4355 8 ай бұрын
Recently I discovered using satisfies with generic types also seems to narrow the type parameters if they would otherwise be inferred as any.
@mamlzy
@mamlzy 9 ай бұрын
thank you!
@rohitsengar9968
@rohitsengar9968 8 ай бұрын
really good topic
@rafadydkiemmacha7543
@rafadydkiemmacha7543 Ай бұрын
Satisfies is super simple, but 99% of the articles about it are so bad and confusing that it's not surprising people don't get it. It's like assert in TDD.
@adi96adi
@adi96adi 8 ай бұрын
Looking forward to that book Matt. Do you plan on pushing out physical copies or will it just be digital?
@mattpocockuk
@mattpocockuk 8 ай бұрын
Physical! Signing with a publisher.
@adi96adi
@adi96adi 8 ай бұрын
@@mattpocockuk hell yeah that's what I wanted to hear
@mike-2342
@mike-2342 9 ай бұрын
Love your work!
@darkwoodmovies
@darkwoodmovies 8 ай бұрын
Good to know, actually. I feel like it should be used as a niche kind of thing more than regularly though. Inferred typing if you can!
@samuelgunter
@samuelgunter 9 ай бұрын
satisfies any
@omergronich778
@omergronich778 9 ай бұрын
The satisfies keyword was one of the things i was missing the most in typescript
@anj000
@anj000 8 ай бұрын
How `as const` plays into this? When to use one over the other or both at the same time?
@anj000
@anj000 8 ай бұрын
Right now I'm using `as const` for my config and I have something like this: ``` const MY_NAMES = { A: 'a', B: 'b' } as const; type MyNames = ValueOf; const MY_CONFIG: Record = { [MY_NAMES.A]: { id: 'a', name: 'a' }, [MY_NAMES.B]: { id: 'b', name: 'b' }, } as const; ``` Does it makes sense to use satisfy in that scenario? Is it providing any benefit?
@eder3232
@eder3232 7 ай бұрын
excelent video
@Metruzanca
@Metruzanca 9 ай бұрын
The title is true. Solidstart used it for its new RouteDefinition and I replaced it with :Routedefinition because I didn't understand it.
@kevinnguyen2249
@kevinnguyen2249 8 ай бұрын
Is the "^?" comment to display the variable type an extension you're using?
@alexanderbrown1178
@alexanderbrown1178 2 ай бұрын
Interesting topic! `satisfies` is a new one on me. What is the difference between using `satisfies` on that `config` object vs just declaring it `as const`. The latter would also narrow the types of properties and prevent mutation of the values, wouldn't it? So what does one do that the other doesn't do?
@mattpocockuk
@mattpocockuk 2 ай бұрын
as const narrows it, satisfies ensures that it matches a constraint. You can use them both together!
@Tarabass
@Tarabass 8 ай бұрын
So with satisfies you just say the object has to satisfy the type declaration, like a template?
@adamcetinkent
@adamcetinkent 8 ай бұрын
So much of TS seems to be about wanting autocomplete options. It just seems very... backwards...
@3Shivs
@3Shivs 7 ай бұрын
You’re great 👍
@jonathangamble
@jonathangamble 9 ай бұрын
Would be cool to see TS tricks for each framework like SvelteKit
@hatemtraif4164
@hatemtraif4164 3 ай бұрын
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?
@Voidstroyer
@Voidstroyer 9 ай бұрын
I'm a bit confused though. What is the difference between const config: {wide: string, narrow: number} = {wide: "100px", narrow: 0} and your example using satisfies?
@Voidstroyer
@Voidstroyer 9 ай бұрын
For example, you mentioned that satisfies is good for keeping the type annotations narrow, while variable annotation is for keeping it wide, however, that would only apply for objects where the keys are not known beforehand if I understand it correctly. Your example with satisfies seems to already have predetermined keys so why would I use satisfies over my own example, or are they the same in this case?
@mattpocockuk
@mattpocockuk 9 ай бұрын
If you have a large config object that you want to make sure is constrained to be a certain type, satisfies is great!
@Voidstroyer
@Voidstroyer 9 ай бұрын
@@mattpocockuk I guess I need to dig in deeper. Satisfies might be better when you are working with an object of which the keys are not known yet and so in that case it might work if you use something like Record. But if you already know the keys and what types they accept, what would the difference be between?: 1. const config: { a: string, b: string | number } = {a: "example", b: 1} 2. const config = { a: "example", b: 1 } satisfies Record
@mattpocockuk
@mattpocockuk 9 ай бұрын
@@Voidstroyer Good question! The difference is one of ergonomics. In your example, every time you add a new key you need to update it in two places - the type and the value. With satisfies, you just update the value and you're good to go.
@Voidstroyer
@Voidstroyer 8 ай бұрын
@@mattpocockuk Oh I see. That's a good point.
@ilkrsrc081
@ilkrsrc081 9 ай бұрын
If the type is known before hand, why would I use satisfies instead of something like this : { [key: "wide" | "narrow"]: string | number }
@mattpocockuk
@mattpocockuk 9 ай бұрын
Note that in your version, both 'wide' and 'narrow' would be string | number. And you'd have a duplication between your types and your runtime (of wide and narrow). satisfies is really useful when you have MASSIVE config objects you need to make sure are the correct shape.
@acrosstundras
@acrosstundras 9 ай бұрын
It's like type validation for variable declaration.
@QwDragon
@QwDragon 9 ай бұрын
It would be great if they had some way of type propagation. Take a look on an issue 54976, please.
@scosee2u
@scosee2u 9 ай бұрын
Nice one!
@Ahmed-fq3kz
@Ahmed-fq3kz 8 ай бұрын
Your channel need some playlists to sort your content based on topics I need to learn more about ts, but don't know what are you talking about in your videos from the title
@tieTYT
@tieTYT 9 ай бұрын
I feel like I can remember what satisfies does or I can remember what as const does, but I can't retain both at the same time. I have a terrible memory, but I think another part of the problem is these keyword names seem arbitrary relative to their behavior. They may as well be a magic number.
@snake1625b
@snake1625b 8 ай бұрын
Satisfies allow you to mutate fields, const you can't mutate
@yadusolparterre
@yadusolparterre 8 ай бұрын
Yeah I still don't get it 😕
@rusicsemenov
@rusicsemenov 8 ай бұрын
Hi, yes it works, but if we take type from object as typeof config, where we will have ab boolean, it can get an error: `Type boolean is not assignable to type false` (you can try to use it)
@mattpocockuk
@mattpocockuk 8 ай бұрын
Could you elaborate?
@rusicsemenov
@rusicsemenov 8 ай бұрын
@@mattpocockuk ```javascript const initialState = { isBoolean: false, someText: 'text', } satisfies Record; type TInitialState = typeof initialState; const state: TInitialState = initialState; state.isBoolean = true; state.someText = 'other text'; ```
@rusicsemenov
@rusicsemenov 8 ай бұрын
In my case something like than, but in the react, where we can use useReducer and later can't update state because boolean can't be as fasle
@dough-pizza
@dough-pizza 9 ай бұрын
I am "satisfied" with your explanation 😀🔫
@riessnico
@riessnico 8 ай бұрын
Can you point out the difference when using as const?
@mattpocockuk
@mattpocockuk 8 ай бұрын
as const makes a value deeply readonly. satisfies ensures that a value matches a given type.
@riessnico
@riessnico 8 ай бұрын
@@mattpocockuk Thanks! Huge fan of your work
@rayan_azzam
@rayan_azzam 9 ай бұрын
This is very useful, 🎉🎉 What maybe the difference from as MyType ?
@mattpocockuk
@mattpocockuk 9 ай бұрын
as MyType acts like ':', but also allows for illegal stuff. It's a way of lying to TS.
@ARLX-yo1wr
@ARLX-yo1wr 9 ай бұрын
Thanks
@platonvin1022
@platonvin1022 8 ай бұрын
i feel like languages that have guides for syntax and it is an actual problem should not exist. Programming should be programming, not spelling, syntax, punctuation or codestyle
@az8560
@az8560 9 ай бұрын
Of course I don't understand 'satisfies', it's literally the first time I see it, and only because of youtube recommendations.
@Wielorybkek
@Wielorybkek 9 ай бұрын
interesting, might be useful
@sinthoras91
@sinthoras91 9 ай бұрын
I think i didnt get the difference between omitting the satisfied keyword on your second example vs specifying it? You have intelisense on both options and type-safety - right?
@acrosstundras
@acrosstundras 9 ай бұрын
Without "satisfies" it would be "{ wide: string; narrow: number }. Satisfies lets you make both of them "string | number".
@mattpocockuk
@mattpocockuk 9 ай бұрын
The difference is that without satisfies, there's no type checking on the value itself. So I could add a boolean there without it erroring.
@jasinrefiku2914
@jasinrefiku2914 9 ай бұрын
The difference is that when you insert keys/values, the keys/values can be only of that `satisfy` type you've inserted, meanwhile, if you omit the satisfies keyword, you can have any type as keys/values. Edit: Hadn't refreshed the video, OP has responded.
@starleaf-luna
@starleaf-luna 8 ай бұрын
... I don't even use typescript; why is this getting recommended to me, alongside _python_ of all languages??
@mattpocockuk
@mattpocockuk 8 ай бұрын
Probably because you're engaging with it?
@WendiCahyono-yf7xc
@WendiCahyono-yf7xc 3 ай бұрын
as the name suggests
@dewke17
@dewke17 9 ай бұрын
How do the “^?” comments work? Is that some sort of vscode extension? I have been looking for it for ages but couldn’t find it. Apart from that, nice video!
@chrisfrancisbass
@chrisfrancisbass 8 ай бұрын
Twoslash Query Comments extension for VS Code
@adiadiadi
@adiadiadi 8 ай бұрын
hey matt, how do you get the type to show up in the comment?
@chrisfrancisbass
@chrisfrancisbass 8 ай бұрын
Try the Twoslash extension for your IDE. I just installed Twoslash Query Comments for VS Code and it looks great.
@adiadiadi
@adiadiadi 8 ай бұрын
thanks
@Bliss467
@Bliss467 9 ай бұрын
I still don’t really see the point of this. You can get inlay hints on members by not explicitly stating the type as record, too, and I suggest you often do intentionally let typescript determine the type for you.
@pxkqd
@pxkqd 9 ай бұрын
If this is the only use case, it seems pretty niche to the point of uselessness
@jasinrefiku2914
@jasinrefiku2914 9 ай бұрын
Depends, if you've got a configuration as the uploader mentioned, and other developers will add stuff into it (you're not the only one, or even if you're the only one, you wanna limit yourself), you would want to limit stuff into being only of specific types.
@danko95bgd
@danko95bgd 9 ай бұрын
thats basically whole typescript, majority is useless
@diadetediotedio6918
@diadetediotedio6918 8 ай бұрын
Every day I feel more and more that TypeScript is a big workaround and messy language
@paxdriver
@paxdriver 9 ай бұрын
Longer is better. Not a fan of these st all tbh
@joshua7884
@joshua7884 9 ай бұрын
weird , typescript became over-engineer javascript linter
@poenaeco
@poenaeco 9 ай бұрын
Yes, because we have a life.
@elina6969
@elina6969 8 ай бұрын
Another thing about satisfies is that using it with well defined interfaces means you don't accidentally widen the type but also get type safety and errors at the correct place. If your config object is passed to a function that expects a Config type but the object does not satisfy Config type, it'll error at the call site, how've you really want it to tell you at the declaration site
@aztecsalvador
@aztecsalvador 3 ай бұрын
Fuq typescript!!
@recursiv
@recursiv 8 ай бұрын
Seems like you could use `satisfies T as T` to get the originally desired behavior too.
@perfectomprg
@perfectomprg 8 ай бұрын
I don’t use JavaScript and I’m the most satisfied 😅
@jonathanadamsson6201
@jonathanadamsson6201 9 ай бұрын
I found it useful sometimes when asserting string literals, say I have something like `type State = 'Idle' | 'Succes' | 'Error'`, then in another place in the code I might have `'Idle' satisfies State`, which will assure the type error is caught if I ever change the string literal There are better ways to handle this typically but in some situations it can be pretty useful
@Di-yes
@Di-yes 8 ай бұрын
Never heard about this. Cool🤔
@CasualCosta
@CasualCosta 8 ай бұрын
1:20 What is that extension that "translate" errors?
@shayanzamani9907
@shayanzamani9907 6 ай бұрын
I guess it's "ts-error-translator", which is developed by Matt!
@scrapsplaysgames
@scrapsplaysgames 9 ай бұрын
One of the big issues I keep encountering with set value objects is if I want to declare a shape on an object or array, I can't also use as const. It loses those exact values. But using as const without setting the shape, it doesn't error if not all values of an enum are used. I wonder if satisfies could help here in some way
@mattpocockuk
@mattpocockuk 9 ай бұрын
Absolutely! {} as const satisfies MyType is what you need.
@QwDragon
@QwDragon 9 ай бұрын
You have some enum and want an array to have a single element per each enum value? Take a look at srtackoverflow questions 59032886 and 58437445.
@i_Amazin_
@i_Amazin_ 9 ай бұрын
Hmm ok. So it's almost like a slightly less strict `as const`. It lets you keep your type strictness (or not leaving it up to inferred), while still having devtool capabilities.
@ShivKumar-qg3mt
@ShivKumar-qg3mt 8 ай бұрын
You don't know how much you helped me with this. I didn't know about this `satisfies` keyword and I had to write duplicate code to get object constraint with autocompletion as typing out `Record` on variable I always loose keys autocompletion.
@dechobarca
@dechobarca 8 ай бұрын
This video satisfies any TypeScript developer. Thumbs up for you Matt.
@YeloPartyHat
@YeloPartyHat 8 ай бұрын
Oh wow, great stuff! Nice explanation and use case!
@Cahnisama
@Cahnisama 9 ай бұрын
This video has been very satisfying, thanks Matt
@samirmishra9946
@samirmishra9946 9 ай бұрын
Happy new year matt! the typescript 🐐
@s4ndeep1203
@s4ndeep1203 9 ай бұрын
Thanks for explaining! I’m still confused about the meaning of widening and narrowing though..
@my_area-fenix9482
@my_area-fenix9482 9 ай бұрын
First
@danylo.s
@danylo.s 9 ай бұрын
TypeScript seems bloated. I use like 10% of what I learned about it in your course. We use Angular for our project.
@specy_
@specy_ 9 ай бұрын
It isn't bloated, it's really expressive and I use 100% of it, if you think about it, the language itself doesn't add even too much on top of js, but it allows you to express things that no other language can
@lohar5055
@lohar5055 9 ай бұрын
Skill issue.
@ra2enjoyer708
@ra2enjoyer708 9 ай бұрын
This can be said for any programming language.
@SkyyySi
@SkyyySi 9 ай бұрын
A programming language containing features you don't use? Wow, it's almost as if the world doesn't center around you and programming languages are made for many different people.
@anhdunghisinh
@anhdunghisinh 9 ай бұрын
Your statement only true with application project. For libraries and utils in which needs lots of generics and advance typed stuff, all those features you deemed unnecessary are very very needed.
Infer is easier than you think
13:38
Matt Pocock
Рет қаралды 90 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 119 М.
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 63 МЛН
Apple peeling hack @scottsreality
00:37
_vector_
Рет қаралды 132 МЛН
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 79 М.
You don't need a frontend framework
15:45
Andrew Schmelyun
Рет қаралды 126 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 176 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 206 М.
A Sudoku Secret to Blow Your Mind - Numberphile
6:08
Numberphile
Рет қаралды 1,7 МЛН
Most React devs don’t understand generic components
5:43
Matt Pocock
Рет қаралды 48 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,7 МЛН
No-Nonsense Backend Engineering Roadmap
10:16
Codebagel
Рет қаралды 202 М.
Node.js Doesn’t Suck Anymore
16:59
Web Dev Simplified
Рет қаралды 111 М.
Don't put your types in .d.ts files
3:54
Matt Pocock
Рет қаралды 136 М.
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 63 МЛН