Code Monkey home page Code Monkey logo

Comments (5)

zavarka avatar zavarka commented on July 24, 2024 1

A version that is a few characters shorter - keyof F | keyof S is the same as keyof F & S. But I could be missing something. :)

type Merge<F, S> = {
  [P in keyof (F & S)]: P extends keyof S
    ? S[P]
    : P extends keyof F
    ? F[P]
    : never;
};

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on July 24, 2024

@zavarka clever 😺 I think there could be some edge-cases with properties in a sense how they will be treated when intersecting them, but for common cases should work, yes.

from type-challenges-solutions.

dgh500 avatar dgh500 commented on July 24, 2024

Using a util function admittedly ( also from the 'Append to Object' challenge ) but the Collapse ( or Flatten ) just satisfies the tests - without it the solution is equivalent.

type Merge<F, S> = Collapse<S & {
  [K in keyof F as K extends keyof S ? never : K]: F[K]
}>

type Collapse<T extends { [k: string]: any }> = {
  [K in keyof T]: T[K]
}

from type-challenges-solutions.

BruceYuj avatar BruceYuj commented on July 24, 2024

Why should we use k extends key F again because k is in keyof F | keyof S

type Merge<F, S> = {
  [P in keyof F | keyof S]: P extends keyof S
    ? S[P]
    : F[P]
};

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on July 24, 2024

@BruceYuj yes, because your key can be either in first set or another one, and you need to filter out one of them.

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.