Code Monkey home page Code Monkey logo

Comments (8)

dvlden avatar dvlden commented on May 28, 2024 1

Very well explained! I did not know that we can assign default value to a type.
I was trying to create a ternary condition (and I failed), instead of using extend via intersect.

from type-challenges-solutions.

RileyMShea avatar RileyMShea commented on May 28, 2024 1

@ghaiklor solution fails all checks on TS versions >= 4.5.5 at the time of writing. I didn't see a obvious reason in the v4.5 release blog. From what I can tell intersections in v4.5+ don't overwrite the optionality(?) or readonly specifiers when that key already exists in the first type.

Something like this TS playground seems necessary now.

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on May 28, 2024

@dvlden thanks! I'm trying to explain as much as I can, but sure, there are some explanations that can be improved (I'm referring to variadic tuple types and Pop, Last).

from type-challenges-solutions.

michaeljsalo avatar michaeljsalo commented on May 28, 2024

Please double check that this solution passes the cases.

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on May 28, 2024

@michaeljsalo cases can be added later after I made the explanation. You can leave the updated solution in the comments if you wish.

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on May 28, 2024

@RileyMShea @martisss has updated the solution with a comment in regard to your issue. Looks like an explanation for the bug. The post is already updated and published.

from type-challenges-solutions.

Siva1432 avatar Siva1432 commented on May 28, 2024

Great explanation, Idk "= keyof T" makes it assign all the keys of T in case not provided. Thanks

from type-challenges-solutions.

daniel-hyun-chae avatar daniel-hyun-chae commented on May 28, 2024

My solution that works with the updated tests.

type MyReadonly2<T, K extends keyof T | null = null> = [K] extends [keyof T]
  ? Omit<T, K> & {
      readonly [key in keyof T]: T[key];
    }
  : {
      readonly [key in keyof T]: T[key];
    };

from type-challenges-solutions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.