Using EXTENDS to constrain generics - Advanced TypeScript

Using EXTENDS to constrain generics - Advanced TypeScript

Matt Pocock

2 года назад

24,510 Просмотров

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


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

@noisypl
@noisypl - 01.06.2022 09:46

Ok, but in what scenario getDeepValue is better than simple obj[key1][key2]? Or that was just an example, but then i would struggle to figure out a good case for what you showed today

Ответить
@enteleform
@enteleform - 01.06.2022 13:48

Great vids! I'm curious to see how you would approach this pattern to accept a dynamic number of keys.

Ответить
@hugodsa89
@hugodsa89 - 02.06.2022 01:07

type ExhaustiveKeyValueMap<
Key,
Value,
key extends Key,
unmappedKey extends Exclude<Key, key>,
value extends Value,
unmappedValue extends Exclude<Value, value>
> = {[k in unmappedKey]: unmappedValue}

- it should only really need to pass the first two generics
- DOES NOT NEED the mapping to be perfectly one to one, but more until either Key or Value runs out of unmappedKey or unmappedValue to supply.

ideas? 😢

Ответить
@farahrayis5928
@farahrayis5928 - 06.06.2022 12:13

Your short videos are so fun. I like how you show how easy is to accomplish tasks in typescript. Thank you.

Ответить
@alexkizer639
@alexkizer639 - 12.06.2022 03:12

I love British people

Ответить
@sumdavias1
@sumdavias1 - 03.07.2022 11:57

Bro I love you

Ответить
@rujor
@rujor - 04.07.2022 11:42

Can't wait for the course 🤩! Please take my money 🤑!

Ответить
@gustavcoetzee5018
@gustavcoetzee5018 - 29.07.2022 04:39

How can use generics to get a instance of a class knowing all my params will be the same but the result will be a different class instance. In example func get(id,group) = ClassA | ClassB | ClassC
Is there a way to call the method. Get<ClassA>(1,'Friends') returns instance ClassA

Ответить
@malvoliosf
@malvoliosf - 30.07.2022 18:06

The `extends` keyword is certainly useful but this is not a good case for it. In the typical use, the arguments to `getDeepValue()` would be unknown strings — obviously, if they were known, you could use ordinary dot-dereference — and the call would not compile.

Ответить
@nazaka9904
@nazaka9904 - 08.09.2022 20:44

Hi. Could you please give me a hint? I'd like to make a function, accepting 2 optional arguments A and B. If argument A provided, request one more argument C. Is it doable with generics?

Ответить
@kishorranga
@kishorranga - 08.11.2022 10:43

Really liked the TypeScript Videos

Ответить
@AndreaPartenope
@AndreaPartenope - 02.01.2024 18:21

why not using just the dot reference ?

Ответить