{"id":18327610,"url":"https://github.com/angular-redux/tassign","last_synced_at":"2025-04-06T01:32:05.927Z","repository":{"id":65412324,"uuid":"73033622","full_name":"angular-redux/tassign","owner":"angular-redux","description":"Subset-typed, non-mutating Object.assign","archived":false,"fork":false,"pushed_at":"2016-11-08T13:22:34.000Z","size":5,"stargazers_count":23,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T02:38:02.487Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/angular-redux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-07T02:05:48.000Z","updated_at":"2020-02-05T02:49:48.000Z","dependencies_parsed_at":"2023-01-22T07:45:15.282Z","dependency_job_id":null,"html_url":"https://github.com/angular-redux/tassign","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/angular-redux%2Ftassign","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular-redux%2Ftassign/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular-redux%2Ftassign/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular-redux%2Ftassign/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/angular-redux","download_url":"https://codeload.github.com/angular-redux/tassign/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423475,"owners_count":20936621,"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":[],"created_at":"2024-11-05T19:11:36.128Z","updated_at":"2025-04-06T01:32:04.636Z","avatar_url":"https://github.com/angular-redux.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# tassign\n\nThis is a simple wrapper for fixed-arity, subset-typed, non-mutating `Object.assign`.\n\n[![CircleCI](https://img.shields.io/circleci/project/angular-redux/tassign/master.svg?maxAge=2592000)](https://circleci.com/gh/angular-redux/tassign/tree/master)\n[![npm version](https://img.shields.io/npm/v/tassign.svg)](https://www.npmjs.com/package/tassign)\n[![npm downloads](https://img.shields.io/npm/dt/tassign.svg)](https://www.npmjs.com/package/tassign)\n\n## Why?\n\nThis came about when writing Redux reducers in TypeScript. Since reducers must not \nmutate the passed in state, a common pattern is to 'set' store properties using `Object.assign()`:\n\n```typescript\ninterface IMyState {\n  foo: Number;\n}\n\nfunction myReducer(state: IMyState, action: Action) {\n  switch(action.type) {\n    case SET_FOO_ACTION: \n      return Object.assign({}, state, { foo: 3 });\n  }\n  \n  return state;\n}\n```\n\nThis works well enough.  `Object.assign` to an empty object produces a new object with the new value\nof `foo`.  However I wasn't happy with it from a type-enforcement perspective.\n\nThe reason is that for a reducer use case, I would like to enforce that the reducer can only set known\nproperties of `IMyState`.\n\nUsing the regular, intersection-based `Object.assign` typings, this is perfectly legal:\n\n```typescript\ninterface IMyState {\n  foo: Number;\n}\n\nfunction myReducer(state: IMyState, action: Action) {\n  switch(action.type) {\n    case SET_FOO_ACTION: \n      // No TS error - I would like there to be.\n      return Object.assign({}, state, { foo: 3, bar: 'wat' });\n  }\n  \n  return state;\n}\n```\n\nFormally, `Object.assign(t:T, u:U)` returns a type which is the _intersection_ of `T \u0026 U`, meaning that properties\ncan be added onto the assignee.  This is reasonable in the general case for `Object.assign`, but it's not what\nI want in a reducer.\n\nIn a reducer, I want `assign(t:T, u:U)` to enforce a subset rule: the return value should be an instance of `T`, and only\nsubsets of `T` are legal values for `U`. In short I want to make sure you can only assign members of `T` to the\nobject returned by the reducer.\n\nSo, here's a utility that handles the typings the way I want in a reducer:\n\n```typescript\ninterface IMyState {\n  foo: Number;\n}\n\nfunction myReducer(state: IMyState, action: Action) {\n  switch(action.type) {\n    case SET_FOO_ACTION: \n      // Returns a new object with the type of the first argument.\n      // Type of the second argument must be a subset of the type of the first argument.\n      \n      // Fails: bar is not a member of IMyState.\n      return tassign(state, { foo: 3, bar: 'wat' });\n  }\n  \n  return state;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangular-redux%2Ftassign","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fangular-redux%2Ftassign","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangular-redux%2Ftassign/lists"}