Coder Social home page Coder Social logo

Optional Values of Type about type-fest HOT 6 OPEN

sindresorhus avatar sindresorhus commented on May 22, 2024 1
Optional Values of Type

from type-fest.

Comments (6)

sindresorhus avatar sindresorhus commented on May 22, 2024 2

Since this seems to be a valid use-case, how would we feel about PRing OptionalKeys in separately to #56?

Sure, but if we add OptionalKeys, we should probably also add RequiredKeys.

from type-fest.

sindresorhus avatar sindresorhus commented on May 22, 2024

What problem are you trying to solve? Why do you need to get the optionals?

from type-fest.

WORMSS avatar WORMSS commented on May 22, 2024

Specifying what the optional defaults should be.

from type-fest.

resynth1943 avatar resynth1943 commented on May 22, 2024

@WORMSS You're probably looking for OptionalKeys, as discussed here #56 (comment)

@sindresorhus Since this seems to be a valid use-case, how would we feel about PRing OptionalKeys in separately to #56?

from type-fest.

resynth1943 avatar resynth1943 commented on May 22, 2024

Also, as for specifying the defaults, if they're all the same, you can do this.

type AWithDefaults = Omit<A, OptionalKeys<A>> & Record<OptionalKeys<A>, Default>;

from type-fest.

dcousens avatar dcousens commented on May 22, 2024

+1 for OptionalKeys and RequiredKeys.
I didn't mind the learning exercise before I found this ticket, but it would have been nice to have it in the library!

My solution was

type OptionalKeys <T> = {
  [K in keyof T]: Pick<Partial<T>, K> extends Pick<T, K> ? K : never
}[keyof T]

Happy to PR, but maybe @resynth1943's answer from #56: {} extends Pick<T, K> ? K : never is nicer.

from type-fest.

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.