Last active
July 30, 2021 15:40
-
-
Save mdesantis/2d6dbf620779e64a4ee279342b9e252d to your computer and use it in GitHub Desktop.
Revisions
-
mdesantis revised this gist
Jul 30, 2021 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,18 @@ // TS Playground link: https://tsplay.dev/wRJl5w interface Props { baseKey: string } 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 = withCustomKey('customInjectedKey')(myProps) console.log(myPropsWithCustomProperty.baseKey) console.log(myPropsWithCustomProperty.customInjectedKey) -
mdesantis created this gist
Jul 30, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)