Created
September 20, 2021 12:34
-
-
Save kitcat-dev/cc8648bafb391228f4fd8ef188e2ffad to your computer and use it in GitHub Desktop.
Revisions
-
kitcat-dev created this gist
Sep 20, 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,15 @@ type Stringified<T> = { [k in keyof T]: T[k] extends number ? string : T[k] }; const stringifyWithKeys = <T, K extends keyof T>(obj: T, ...keys: K[]): Stringified<T> => { const stringifiedObject = obj as Stringified<T>; for (const [key, value] of Object.entries(pick(stringifiedObject, ...keys))) { if (value !== undefined && typeof value === 'number') { stringifiedObject[key as K] = String(value) as T[K] extends number ? string : T[K]; } } return stringifiedObject; }; stringifyWithKeys({ id: 2, name: 'Albert', age: 25 }, 'id') // { id: '2', name: 'Albert', age: 25 }