TypeScript Opaque Types // Nominal vs Structural Typing (Advanced TypeScript)

  Рет қаралды 4,038

basarat

basarat

Күн бұрын

Пікірлер: 30
@noccer
@noccer 2 жыл бұрын
Brilliant! ☘️ But also - you might need to fire the Script Supervisor on your film set - where did your Fila jumper disappear to at the end of the video? 😜 Thanks Bas 👍
@basarat
@basarat 2 жыл бұрын
Sometimes I even end up with a completely different haircut - and sometimes - shaved head 😂 PS: always great hearing from you
@noccer
@noccer 2 жыл бұрын
@@basarat legend! I see it as a feature, not a bug. Next time swap out your glasses or something, see if we spot it 😄 Cheers ☘️
@akhiltheloquacioussoul
@akhiltheloquacioussoul 2 жыл бұрын
Quite informative. Well explained too. Thanks a lot.
@basarat
@basarat 2 жыл бұрын
Thanks ! Also thanks for being a public subscriber
@ftnsco
@ftnsco Жыл бұрын
This is gold :)
@sisiradhikari9577
@sisiradhikari9577 Жыл бұрын
Learning a lot! Thank you for your informative videos!
@tusharkhairnar9142
@tusharkhairnar9142 Жыл бұрын
Great solution to daily programming problems. Thanks! :)
@basarat
@basarat Жыл бұрын
Thank _you_ for leaving a comment :)
@dennisgonzales9521
@dennisgonzales9521 2 жыл бұрын
thanks!
@basarat
@basarat 2 жыл бұрын
Thanks for leaving a comment
@cyrusgracias4556
@cyrusgracias4556 2 жыл бұрын
Amazing, I thought the issue was that now we can pass negative number, NaN, Infinity to those number types
@basarat
@basarat 2 жыл бұрын
Good point . Sadly NaN is always a valid number in TypeScript 🥲 For the curious its because in JavaScript: console.log(typeof NaN); // 'number'
@avi3681
@avi3681 2 жыл бұрын
@Cyrus Since the opaque type makes you produce the value in a special constructor function, you can add extra runtime validation inside that function and return an optional result. Then if the return is non-empty you are guaranteed that is is non-negative, non-NaN, etc.
@benjidaniel5595
@benjidaniel5595 2 жыл бұрын
I really wish TS would make opaque types a 1st class feature (like Flow does). Workarounds like this are okay, but not the prettiest.
@basarat
@basarat 2 жыл бұрын
I agree 👍🏻
@benjidaniel5595
@benjidaniel5595 2 жыл бұрын
I feel like I’ve been watching one particular thread on GH about it for years, and each release I think “will this be the one they implement it”, but alas it seems be forever stuck in the “todo next” part of the roadmap.
@basarat
@basarat 2 жыл бұрын
@@benjidaniel5595 I know the feeling it took 6 years to get `override` into TypeScript kzbin.info/www/bejne/l6PNo4KaiZxmmJY 😂
@boian-inavov
@boian-inavov Жыл бұрын
Or you can pass in an object with both entries as part of it and then destructure it accordingly to use in your function. This way you don’t need to have such a hacks approach of additional methods, nor strange types. Interesting video though
@dimitargetsov9690
@dimitargetsov9690 4 ай бұрын
@boian-inavov . The only (edinstveniat) comment (komentar) that matter (koito e ot znachenie) . Congrats (pozdravi) .
@c0mpuipf
@c0mpuipf 2 жыл бұрын
who needs opaque types in a normal app? just asking cause i have no idea how this is useful other than it being food for thought;
@basarat
@basarat 2 жыл бұрын
I personally don't over use them. Only use them with DB objects when `ids` are being passed around (instead of full record objects). Help provide context on what a particular "id" string is a reference to. Hope that helps
@ViktorKarpov-o8h
@ViktorKarpov-o8h 6 ай бұрын
If u do this, u will produce more 'indian code'. Do not ever do this in real codebase
@basarat
@basarat 6 ай бұрын
I’ve seen some pretty bad code independent of race and color, but okay, sure 😂 This is me assuming you can’t spell Indian 🤦🏻‍♂️
@yegorzakharov8514
@yegorzakharov8514 Жыл бұрын
Ugh, that's disgusting. Whyyy do duck typing. Good fix though, thanks.
@basarat
@basarat Жыл бұрын
Thanks! When TypeScript is too strict => Traditional Dynamic JS People hate. When TypeScript is too loose => Traditional TypeSafe Language People hate. Its a fine balance to achieve maximal People Love But yeah, I'm in the prefer nominal Typing Camp, with perhaps some special syntax to explicitly specify type structural compatability 🤷🏻‍♂️ Or perhaps some special syntax to offer optional strict nominal typing for some types 🌹
@yegorzakharov8514
@yegorzakharov8514 Жыл бұрын
@basarat that's true. Especially when you consider how ubiquitous JS is. But I just want TS to be strict. There are people who are scared of TS because of the strictness, therefore making it stricter wouldn't change much. Plus you could make TS super strict with flags. You want nominal types, opt in! I'm sure it has its own issues, but at least it's good for both camps.
@Luxcium
@Luxcium 2 жыл бұрын
😅 He is talented and he is smart but with this hoodie he is just making me melt 🫠🥰 ✨
@Luxcium
@Luxcium 2 жыл бұрын
Sorry I should probably behave like I am not a _primitive type_ … and trying not to treat people like they are _object_ …
@basarat
@basarat 2 жыл бұрын
😂🌹
Use Cases for Remainders in Programming (with JavaScript)
4:40
TypeScript TYPES vs INTERFACES (Key Differences)
7:16
basarat
Рет қаралды 95 М.
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 34 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН
An Option type in TypeScript (inspired by Rust)
12:53
Andrew Burgess
Рет қаралды 17 М.
TypeScript Template Literal Types // So much power ☢️
5:18
Never in Conditional Types // Advanced TypeScript Hack
3:43
Mapped Types - Advanced TypeScript
12:16
Dmytro Danylov
Рет қаралды 43 М.
Branded Types give you stronger input validation
9:22
Andrew Burgess
Рет қаралды 18 М.
TypeScript Mapped Types as clauses // Hidden Gem
4:27
basarat
Рет қаралды 5 М.
TypeScript Enums are TERRIBLE.  Here's Why.
14:05
Michigan TypeScript
Рет қаралды 68 М.
Mapped type MODIFIERS in TypeScript - Advanced TypeScript
4:16