Last active
May 2, 2021 22:49
-
-
Save gtrabanco/1f9aaac355e4cdd3a4acd38f08661795 to your computer and use it in GitHub Desktop.
Solving Index Signature in Typescript for dynamic object property names
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 characters
| /** | |
| * In this case this function convert an objest to url query params | |
| */ | |
| const object2UrlParameters = (objectValues:object): string => { | |
| let returnedParams:string = '' | |
| Object.getOwnPropertyNames(objectValues).forEach((paramName) => { | |
| let key = paramName as keyof object //Avoiding Index Signature | |
| let value = objectValues[key] | |
| returnedParams = `${returnedParams}&${paramName}=${value}` | |
| }) | |
| return returnedParams.slice(1) | |
| } | |
| //Example | |
| console.log(object2UrlParameters({foo: "baz", number: 2})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment