old TypeScript syntax I just discovered

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2023
  • This is an exceedingly nerdy dive into the topics of covariance and contravariance in TypeScript.
    Prefer to read about this topic? Read the accompanying blog post here: shaky.sh/ts-covariance-contra...
    Check out the code for this video here: tsplay.dev/mqypYm
    My Links
    shaky.sh
    shaky.sh/tools
    / andrew8088
    mastodon.social/@andrew8088
    #programming #coding #typescript #javascript #covariance #contravariance
    #frontenddeveloper #backenddeveloper #softwareengineer #softwareengineering #softwaredevelopment
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 24

  • @MichiganTypeScript
    @MichiganTypeScript 10 หลายเดือนก่อน +15

    so cool to see some coverage on this! it's been on our list for a while as part of a "TypeScript features you almost definitely haven't seen" sorta-mini-series. maybe we'll have to have you back on the MiTS channel when we get around to this one! Great stuff!

    • @offroaders123
      @offroaders123 10 หลายเดือนก่อน +1

      Woah, fancy seeing you guys here!

    • @MichiganTypeScript
      @MichiganTypeScript 10 หลายเดือนก่อน +1

      @@offroaders123 haha and same to you!

  • @Luxcium
    @Luxcium 3 หลายเดือนก่อน

    I know that you have a life and ALL... but also you have such a charismatic presence in your videos... You should own it and be willing to make more videos (I hope you could find the time to do that if it makes you happy and are into it)

  • @zorzysty
    @zorzysty 7 หลายเดือนก่อน

    Can't wait for new videos!

  • @NoiseLeGGa
    @NoiseLeGGa 10 หลายเดือนก่อน +10

    In this video, the word "job" is said 83 times.

  • @offroaders123
    @offroaders123 10 หลายเดือนก่อน +1

    This is great! Glad you made a video about this.

  • @dimitrimitropoulos
    @dimitrimitropoulos 10 หลายเดือนก่อน +2

    re: your question on uses for this, my understanding is that the reason this exists (especially in a structural type system) is for performance: it allows the compiler to take "the happy path" a lot faster rather than having to solve all the ways the type could be used. I would agree that it's nice to document to your users how you intend generic inputs to be used but then there's the problem of almost no one knowing about this :P
    great video!

    • @yankee-in-london
      @yankee-in-london 9 หลายเดือนก่อน +1

      yes i remember when this came out ... the main focus was giving library authors the ability to make their types more performant

  • @elmalleable
    @elmalleable 10 หลายเดือนก่อน +2

    The depth of knowledge on that priority job boils down to when your network provider yells you you have unlimited data till you run up to the limit and realized you were gas lit. They told you A but A has a catch. Except ts is like let your executor A be just A and nothing but the A. So help me binary.
    I didn't go over that part 5 times

    • @elmalleable
      @elmalleable 10 หลายเดือนก่อน +1

      Just explanation for future me if I ever have to hunt down this
      E2 error because a restaurant can't say they serve hot and cold drinks and only actually give you cups for cold drinks. Cos a hot drink will shatter the cold drink cup when someone attempts to do that
      E3 works because the restaurant serves cold drinks but they give you a cup that works fine for both hot and cold drinks and using that cup will work perfectly

  • @junyorff12
    @junyorff12 7 หลายเดือนก่อน

    Great.

  • @lengors7327
    @lengors7327 10 หลายเดือนก่อน

    7:15 my guess is because of existing types like array. Array is by default covariant on T. If typescript were to complain there than Array couldn't be covariant on T, rather invariant on it. Which imo would make more sense and then they could just make ReadonlyArray covariant.

  • @samirnayibruza9135
    @samirnayibruza9135 9 หลายเดือนก่อน

    🤯🤯🤯

  • @user-kn4wt
    @user-kn4wt 10 หลายเดือนก่อน

    interesting!

  • @AK-vx4dy
    @AK-vx4dy 10 หลายเดือนก่อน

    wow.. first time i start to have some foggy clue what covariance and contravariance mens
    Excelent ;)

  • @ionelCristianLupu_
    @ionelCristianLupu_ 10 หลายเดือนก่อน

    4:56 For e3, what if I want to access the `level` property from that job? If I pass a "normal" job, the `level` property won't exist, right?

  • @KubaBogaczewicz
    @KubaBogaczewicz 10 หลายเดือนก่อน +1

    How come typescript made Array covariant? Arrays should be - as probably all collections.
    type A = {a: string}
    type B = A & {b: string}
    declare const ab: Array;
    let bad: Array = ab; //so the covariant behaviour from the video
    bad[0] = {a: "hello"} as A; // since it's now Array we can put A into it
    const wrongType: B = ab[0]; // but now we get out this value from ab, so it would be B. Except we've put A there through `bad`

    • @lengors7327
      @lengors7327 10 หลายเดือนก่อน

      Yeah, this shouldnt be possible. Imo Array should be invariant and only ReadonlyArray should be covariant

    • @IvanKleshnin
      @IvanKleshnin 10 หลายเดือนก่อน

      Another good example of unsound "I don't care" behavior.

  • @feldinho
    @feldinho 10 หลายเดือนก่อน

    Covariance and contravariance are such bad names! I get it using strange mathy names for things like monads and semigroups, that are big concepts, but type narrowing and widening are more than good enough in this case! (not a criticism about you. great video as always!)

    • @feldinho
      @feldinho 10 หลายเดือนก่อน +1

      Also, knowing that co is latin for “with” and contra is “against” doesn’t help at all! :P

  • @user-zg5rm7wl7o
    @user-zg5rm7wl7o 8 หลายเดือนก่อน

    type State = {
    get: (key: string) => T | null;
    set: (key: string, value: T) => void;
    }
    export const useLocalStorage = (prefix: string): State => {
    return {
    get:(key) => {
    return JSON.parse(window.localStorage.getItem(prefix + key) || "null");
    },
    set: (key, value ) => {
    window.localStorage.setItem(prefix + key, JSON.stringify(value));
    },
    };
    };