will i never understand this? unknown.

Поделиться
HTML-код
  • Опубликовано: 25 мар 2024
  • i think I only said "covariance" when I meant "contravariance" once ...
    My Links
    shaky.sh
    shaky.sh/tools
    #programming #coding #typescript #javascript #covariance #contravariance
    #frontenddeveloper #backenddeveloper #softwareengineer #softwareengineering #softwaredevelopment
  • НаукаНаука

Комментарии • 33

  • @veritatas678
    @veritatas678 3 месяца назад +24

    My little helper is thinking "covariant" = "I take at most" "contravariant" = "I take at least"

  • @michaelns9887
    @michaelns9887 3 месяца назад +6

    It would be also easier to understand if we add an implementation to `registerUser`
    function registerUser(greetFn: (names: string) => void) {
    greetFn("John")
    }
    This is completely correct. But if `greetFn` is `greetUser` - then it will break, because it can only accept Role.

  • @MichiganTypeScript
    @MichiganTypeScript 3 месяца назад +16

    We asked Ryan and Daniel (from the TypeScript) team about this exact thing and Ryan said he presumes that whoever wrote the ReturnType utility didn't think the false arm was reachable. The video is called "replacing `any` with `unknown` everywhere - the story of the failed `strictAny` flag" (RUclips gets fussy if I post a link).
    As always, fantastic content, Andrew!

    • @xorlop
      @xorlop 3 месяца назад +2

      Omg of course Michigan typescript out here with the knowledge.

    • @offroaders123
      @offroaders123 3 месяца назад

      Yaay we got everyone here 🎉

  • @Matt23488
    @Matt23488 3 месяца назад +5

    I've stumbled with this many times over the years and am at the point where I understand it at an abstract level, but the box analogy I thought was fantastic and really made an intuitive connection for me.

  • @spead
    @spead 3 месяца назад +1

    Great video! Difficult concept explained intuitively.

  • @pratik245
    @pratik245 Месяц назад

    In other languages like java, supertypes can always have reference to subtype instance. So string having referemce to a role is not surprising but has a different name calles contravariance in typescript.

  • @AlexanderYaremchuk
    @AlexanderYaremchuk 3 месяца назад

    One thought I got - function is a worker.
    Input - is a field which is cultivated by him.
    Output - checks to be paid returned by the worker.
    so we can substitute a worker with another worker who cultivates same or bigger field and returns same or narrower amount of checks.

  • @ashleyredman
    @ashleyredman 3 месяца назад

    That was a great video, and the moving box example landed it for me perfectly

  • @kcin4206
    @kcin4206 Месяц назад

    Not many in-depth videos on this topic. Good video 👍

  • @xorlop
    @xorlop 3 месяца назад +1

    Now I want to learn more about all the rules where co and contravariance apply! I think this was a great intro.

  • @laesseV
    @laesseV 3 месяца назад +1

    The title is a very good javascript pun, will use it in my next pr review.

  • @GHOST8799
    @GHOST8799 3 месяца назад

    Huh I wonder if the concept of covariant and contravariants apply when dealing with function overloads? Specifically in how we create function overloads

  • @BradySie-wo6vf
    @BradySie-wo6vf 3 месяца назад

    another possible helpful way of thinking about it is if you flatten the passed function and inline it, you're expecting to pass it a Role, so if the actual function expects a string then of course a Role can be passed into the string

  • @larshanisch
    @larshanisch 3 месяца назад

    Very good explanation. What helps me, is to make clear, who "calls" the provided function, in your case the registerUser function will call it. And if can be called with an arbitrary string, then you cannot pass in a function which only knows how to handle a Role (some specific strings). But a function which accepts any string is good enough to get a Role as an argument. It needs time to get used to it.

  • @PyroVox
    @PyroVox 3 месяца назад

    Thanks so much for making this video. Having grappled with this exact issue when trying to use generics in TypeScript, your explanation of covariance and contravariance and the moving box example are the best I've come across yet. I still don't *fully* understand the mechanism, at least enough to be able to explain it to someone else as well as you do in this video, but my internal understanding is much improved. I'd love to see more videos like this where you dive into similar problems in TypeScript and maybe even other languages.

  • @feldinho
    @feldinho 3 месяца назад +3

    These ideas are easy but the names are beyond terrible.
    I like “input widening” and “output narrowing” much better, since function/type A can be replaced with B if the latter receives the same arguments as A or wider, and returns the same type or narrower.
    I know jargon has its place, but we are so far removed from category theory that it only gets in the way, in this case.

  • @gordonfreimann
    @gordonfreimann 3 месяца назад +1

    Yikes. What a nightmare this language is. God i feel good writing Go

  • @devinorium
    @devinorium 3 месяца назад

    Challenging concept. Awesome explanation. This proves that there is a set of unknown cardinality of unrealized RUclips videos about hard and interesting TS subjects (❁´◡`❁)

  • @Buddharta
    @Buddharta 3 месяца назад

    I feel this is pretty natural in the context of category theory

  • @helleye311
    @helleye311 3 месяца назад

    Great explanation. This always hurts my head for the first 5 minutes I think about it. After that it kind of makes sense, but if I stop thinking about it it's back to being unintuitive.
    This video might help with that though, we'll see if it sticks.

  • @zorzysty
    @zorzysty 3 месяца назад

    Finally an explanation of this topic that I actually understand! Thank you Andrew!

  • @danielbark
    @danielbark 3 месяца назад +1

    Amazingly explained!

  • @rak3shpai
    @rak3shpai 3 месяца назад

    That was an awesome explanation. Thanks.

  • @jeromesnail
    @jeromesnail 3 месяца назад

    Great video.

  • @ole4707
    @ole4707 3 месяца назад

    Array is covariant in T? That seems highly unsound. That means I can pass my Role[] to some function foo(strings: string[]), which can then do any operation valid for a string[] to it, including adding any string at all (not just Role strings) to my Role[]. Is there some mechanism in the type system that I don't know about preventing something like this from happening?

    • @DavidAguileraMoncusi
      @DavidAguileraMoncusi 3 месяца назад

      Probably using ReadonlyArray?

    • @ole4707
      @ole4707 3 месяца назад

      @@DavidAguileraMoncusiI think that sort of misses the point. It's not about mutation, but rather about how easy it is to violate the constraints of the defined types, even on accident. The problem I pose may not exist for ReadonlyArray, presumably because it happens to be immutable, but it's still an issue for Array. It just seems like Array has the wrong variance constraints.

    • @DavidAguileraMoncusi
      @DavidAguileraMoncusi 3 месяца назад +1

      @@ole4707 You're absolutely right. When I first saw the example, I was thinking of the array type as being immutable. But it isn't, of course. So making it covariant is (AFAICT) a clear bug. If you stick to immutable types, though, then things should work as expected, shouldn't they?

    • @sp3ctum
      @sp3ctum 3 месяца назад +1

      I have run into this before. I don't know if there is a fix.
      I think the best thing to do is to avoid or limit the use of mutation.

  • @tthiagolino8
    @tthiagolino8 3 месяца назад

    Finally an explanation that I could understand