Skip to content

Instantly share code, notes, and snippets.

@mdesantis
Last active July 30, 2021 15:40
Show Gist options
  • Select an option

  • Save mdesantis/2d6dbf620779e64a4ee279342b9e252d to your computer and use it in GitHub Desktop.

Select an option

Save mdesantis/2d6dbf620779e64a4ee279342b9e252d to your computer and use it in GitHub Desktop.

Revisions

  1. mdesantis revised this gist Jul 30, 2021. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,18 @@
    // TS Playground link: https://tsplay.dev/NlpexN
    // TS Playground link: https://tsplay.dev/wRJl5w

    interface Props {
    baseKey: string
    }

    const withCustomProperty2 = <K extends string>(customKey: K) => {
    const withCustomKey = <K extends string>(customKey: K) => {
    return <P>(props: P): P & { [k in K]: boolean } => {
    return { ...props, [customKey]: true } as P & { [k in K]: boolean }
    }
    }

    const myProps: Props = { 'baseKey': 'some value' }
    const myPropsWithCustomProperty = withCustomProperty2('customInjectedKey')(myProps)
    const myPropsWithCustomProperty = withCustomKey('customInjectedKey')(myProps)

    console.log(myPropsWithCustomProperty.baseKey)
    console.log(myPropsWithCustomProperty.customInjectedKey)

  2. mdesantis created this gist Jul 30, 2021.
    17 changes: 17 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // TS Playground link: https://tsplay.dev/NlpexN

    interface Props {
    baseKey: string
    }

    const withCustomProperty2 = <K extends string>(customKey: K) => {
    return <P>(props: P): P & { [k in K]: boolean } => {
    return { ...props, [customKey]: true } as P & { [k in K]: boolean }
    }
    }

    const myProps: Props = { 'baseKey': 'some value' }
    const myPropsWithCustomProperty = withCustomProperty2('customInjectedKey')(myProps)

    console.log(myPropsWithCustomProperty.baseKey)
    console.log(myPropsWithCustomProperty.customInjectedKey)