{"id":15899737,"url":"https://github.com/ciricc/tst-defaults","last_synced_at":"2026-01-03T17:09:53.305Z","repository":{"id":37485964,"uuid":"501844853","full_name":"ciricc/tst-defaults","owner":"ciricc","description":"Make your TypeScript defaults expectable with tst-reflect and ttypescript","archived":false,"fork":false,"pushed_at":"2022-07-04T17:30:17.000Z","size":231,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-14T10:12:37.360Z","etag":null,"topics":["defaults","reflect","reflection","ttypescript","types","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ciricc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-10T00:05:07.000Z","updated_at":"2022-08-12T00:52:22.000Z","dependencies_parsed_at":"2022-07-12T16:19:11.818Z","dependency_job_id":null,"html_url":"https://github.com/ciricc/tst-defaults","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ciricc%2Ftst-defaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ciricc%2Ftst-defaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ciricc%2Ftst-defaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ciricc%2Ftst-defaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ciricc","download_url":"https://codeload.github.com/ciricc/tst-defaults/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219869615,"owners_count":16555482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["defaults","reflect","reflection","ttypescript","types","typescript"],"created_at":"2024-10-06T10:23:03.568Z","updated_at":"2026-01-03T17:09:53.265Z","avatar_url":"https://github.com/ciricc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Make your defaults expectable\n\nThis project uses [tst-reflect](https://github.com/Hookyns/tst-reflect) library for making your schemas expectable. For example, if you are parsing JSON with predefined schema but didn't want to check that some fields is not defined in object and want to just get default values.\n\n## Simple example\n\nThis simple example shows how much problems we have when parsing JSON in TypeScript.\n\n```ts\ntype JSONSchema = {\n    n: number\n    level: {\n        n2: number\n        bool: boolean\n    },\n    list: number[],\n    b: boolean\n}\n\nconst parsedJson = JSON.parse('{}') as JSONSchema;\n\nconsole.log(parsedJson.level.n2 + 1) // error, level is undefined\nconsole.log(parsedJson.list.map(el =\u003e el + 1)) // error, list is undefined and has no method map\nconsole.log(parsedJson.n + 1) // NaN\n\nif (parsedJson.b) { // it may have error, because undefined is not a false \n    makeRiskCall()\n}\n\nif (paredJson.b === true) { // why we need this in TypeScript?? So much code...\n    makeRiskCall()\n}\n\n```\n\nBut you may say that we have a solution?\n\n```ts\nclass JSONSchema {\n    public n: number = 0;\n    public level: {\n        n2: number\n        bool: boolean\n    } = {\n        n2: 0,\n        bool: false,\n    }\n    public list: number[] = [];\n    public b:boolean = false;\n    constructor(v:any) {\n        Object.assign(this, v);\n    }\n}\n\nconst parsedJson = new JSONSchema(JSON.parse('{\"level\": {\"n2\": 1}}'));\n\nconsole.log(parsedJson.n + 1) // ok\nconsole.log(parsedJson.level.n2 + 1) // ok, result is 2\nconsole.log(parsedJson.level.bool) // undefined\n```\n\nNot so bad, but still not good. And we have much code again.\n\n## Solution\n\n```ts\nimport { mergeDefaults } from \"tst-defaults\";\n\ntype JSONSchema = {\n    n: number\n    level: {\n        n2: number\n        bool: boolean\n    },\n    list: number[],\n    b: boolean\n}\n\nconst parsedJson = mergeDefaults\u003cJSONSchema\u003e(JSON.parse('{\"level\":{\"bool\":true}}'));\n\nconsole.log(parsedJson.b === false) // ok\nconsole.log(parsedJson.list.map(el =\u003e el + 1)) // ok\nconsole.log(parsedJson.level.n2 + 1 === 1) // ok\nconsole.log(parsedJson.level.bool == true) // ok\nconsole.log(parsedJson.n + 1) // ok\n```\n\nSimple and fast.\n\n## Installation\n\n```shell\nyarn add --dev typescript ttypescript tst-reflect-transformer ts-node\n```\n\n```shell\nyarn add tst-defaults\n```\n\nSetup your `tsconfig.json`\n\n```js\n{\n    \"ts-node\": {\n      \"compiler\": \"ttypescript\"\n    },\n    \"compilerOptions\": {\n        ...,\n        \"plugins\": [\n            {\n                \"transform\": \"tst-reflect-transformer\"\n            }\n        ]\n    }\n}\n```\n\nSetup `package.json`\n\n```json\n{\n    \"scripts\": {\n        \"start\": \"ts-node index.ts\",\n        \"build\": \"ttsc\"\n    }\n}\n```\n\n## API\n\nSome api documentation.\n\n### useDefault\n\nSaves type default value in storage for next time resolving.\n\n```ts\nimport { useDefault, mergeDefaults } from 'tst-defaults';\n\ntype Duration = {\n  seconds: number;\n};\n\ntype Schema = {\n  expires: Duration;\n  options: Object;\n  users: number;\n};\n\nuseDefault\u003cDuration\u003e({ seconds: -1 });\nconst config = mergeDefaults\u003cSchema\u003e(JSON.parse('{}'));\n\nconsole.log(config.expires); // { seconds: -1}\nconsole.log(config.users); // 0\nconsole.log(config.options); // {}\n```\n\n### mergeDefaults\n\nMerging default values into variable value. If variable is undefined, than it will create default value for it.\n\n```ts\nimport { mergeDefaults } from \"tst-defaults\";\n\nconst jsonErrorIgnored = ():any =\u003e {}\n\ntype Schema = {\n    n: number\n};\n\nconst config = mergeDefaults\u003cSchema\u003e(jsonErrorIgnored());\nconsole.log(config.n) // 0, config is created as object\n```\n\n### getDefaultValue\n\nReturns default value for type.\n\n```ts\nimport { getDefaultValue } from \"tst-defaults\";\n\nconsole.log(getDefaultValue\u003cstring\u003e()) // \"\"\n\nenum Person {\n    UNKNOWN = 0,\n    PRIVATE = 1,\n    PUBLIC = 2,\n}\n\nconsole.log(getDefaultValue\u003cstring\u003e()) // \"\"\nconsole.log(getDefaultValue\u003cboolean\u003e()) // false\nconsole.log(getDefaultValue\u003cObject\u003e()) // {}\nconsole.log(getDefaultValue\u003cnumber\u003e()) // 0\nconsole.log(getDefaultValue\u003cArray\u003cnumber[]\u003e\u003e()) // []\nconsole.log(getDefaultValue\u003cFunction\u003e()) // null\nconsole.log(getDefaultValue\u003cPerson\u003e() == Person.UNKNOWN) // true\n```\n\n### deleteDefault\n\nRemoves type default value from storage.\n\n```ts\nimport { deleteDefault, useDefault, getDefaultValue } from \"tst-defaults\";\n\ntype Duration = {\n  seconds: number;\n};\n\nuseDefault\u003cDuration\u003e({\n    seconds: -1\n});\n\nconsole.log(getDefaultValue\u003cDuration\u003e();) // {seconds: -1}\n\ndeleteDefault\u003cDuration\u003e();\nconsole.log(getDefaultValue\u003cDuration\u003e();) // {}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fciricc%2Ftst-defaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fciricc%2Ftst-defaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fciricc%2Ftst-defaults/lists"}