Title: Add pure and immutable keywords to ensure code has no unintended side-effects · Issue #17181 · microsoft/TypeScript · GitHub
Open Graph Title: Add pure and immutable keywords to ensure code has no unintended side-effects · Issue #17181 · microsoft/TypeScript
X Title: Add pure and immutable keywords to ensure code has no unintended side-effects · Issue #17181 · microsoft/TypeScript
Description: The aim of this proposal is to add some immutability and pure checking into the typescript compiler. The proposal adds two new keywords that would give developers a means to define functions that are pure - meaning that the function has ...
Open Graph Description: The aim of this proposal is to add some immutability and pure checking into the typescript compiler. The proposal adds two new keywords that would give developers a means to define functions that a...
X Description: The aim of this proposal is to add some immutability and pure checking into the typescript compiler. The proposal adds two new keywords that would give developers a means to define functions that a...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17181
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add pure and immutable keywords to ensure code has no unintended side-effects","articleBody":"The aim of this proposal is to add some immutability and pure checking into the typescript compiler. The proposal adds two new keywords that would give developers a means to define functions that are pure - meaning that the function has no-side effects, and define variables that are immutable - meaning that they can never be used in an impure context.\r\n\r\n### Pure\r\nThe `pure` keyword is used to define a function with no side-effects ([it is allowed in the same places as the `async` keyword](https://tc39.github.io/ecma262/#sec-async-function-definitions)).\r\n```TS\r\npure function x(arg) {\r\n return arg\r\n}\r\n```\r\nThe keyword should be not be emitted into compiled javascript code.\r\n\r\nA pure function:\r\n- May not call an function not tagged as pure in the context of arguments, or this.\r\n - `this.nonPure()`, `arg.nonPure()`, `nonPure(arg)`, and `nonPure(this)` are all disallowed.\r\n- May make non-pure calls on instance variables, as the non-pureness applies to instance variables, so the function is technically still side-effect free:\r\n```JS\r\npure function x() {\r\n const arr = []\r\n arr.push(1)\r\n return arr\r\n}\r\n```\r\n - - _this one i'm not entirely sure about, but it seems like it would be very hard to build pure functions without it._\r\n - - _languages like elm get around this by having a push function which returns a new array._\r\n- Modify the input variables\r\n - `arg.x = 1` is disallowed within the function body.\r\n- Modify variables on `this`\r\n - `this.y = 1` is disallowed within the function body.\r\n- Must return a value (otherwise there's no point to the function!).\r\n\r\n### Immutable\r\nSimilarly a variable may be tagged as immutable:\r\n`immutable x = []` (maybe keyword should be shortened to `immut`, or the `pure` keyword could be reused for consistency?).\r\nThis keyword is replaced with `const` in emitted code.\r\n\r\nAn immutable variable:\r\n- Is treated as if it were `const` (i.e. its reference may not be reassigned).\r\n- May not have any impure instance methods called on it.\r\n - `x.nonPure()` is disallowed.\r\n- May not be passed as an argument to impure functions.\r\n - `nonPure(x)` is disallowed.\r\n- May not be reassigned to a variable reference that is also not marked as pure.\r\n - `const y = x;` is disallowed.\r\n - `const y = { z: x }` is disallowed.\r\n- May not have instance properties set on it.\r\n - `x.foo = 1` is disallowed.\r\n- May be passed to pure functions.\r\n - `pureFn(x)` is allowed.\r\n- May have pure instance methods called on it.\r\n - `x.toString()` is allowed.\r\n- For arrays, its type is strictly set at definition time, meaning that element-wise type checks will pass (fixes: https://github.com/Microsoft/TypeScript/issues/16389)\r\n - i.e. the following code will now pass\r\n```TS\r\npure function fn(arg : ('a' | 'b')[]) { }\r\n\r\nimmutable x = ['a', 'b']\r\nfn(x)\r\n```\r\n\r\n### With objects/interfaces\r\nThe keyword(s) should also be allowed in `object` (and by extension `interface`) definitions:\r\n```TS\r\nconst obj1 = {\r\n // immutable and non-pure\r\n immutable fn1: function () { },\r\n \r\n // mutable and pure\r\n fn2: pure function () { },\r\n\r\n // immutable and pure\r\n immutable fn3: pure function () { },\r\n\r\n // immutable and non-pure\r\n immutable fn3() { },\r\n\r\n // immutable and pure\r\n pure fn3() { },\r\n}\r\n\r\ninterface IFace {\r\n pure toString() : string // pure must always return a value\r\n\r\n immutable prop : number\r\n\r\n immutable pure frozenFn() : boolean\r\n}\r\n```\r\n\r\n### With existing typings\r\nWith this proposal, the base javascript typings could be updated to support it.\r\nI.e. the array interface would become:\r\n```TS\r\ninterface Array\u003cT> {\r\n pure toString(): string;\r\n pure toLocaleString(): string;\r\n pure concat(...items: T[][]): T[];\r\n pure concat(...items: (T | T[])[]): T[];\r\n pure join(separator?: string): string;\r\n pure indexOf(searchElement: T, fromIndex?: number): number;\r\n pure lastIndexOf(searchElement: T, fromIndex?: number): number;\r\n pure every(callbackfn: (this: void, value: T, index: number, array: T[]) => boolean): boolean;\r\n pure every(callbackfn: (this: void, value: T, index: number, array: T[]) => boolean, thisArg: undefined): boolean;\r\n pure every\u003cZ>(callbackfn: (this: Z, value: T, index: number, array: T[]) => boolean, thisArg: Z): boolean;\r\n pure some(callbackfn: (this: void, value: T, index: number, array: T[]) => boolean): boolean;\r\n pure some(callbackfn: (this: void, value: T, index: number, array: T[]) => boolean, thisArg: undefined): boolean;\r\n pure some\u003cZ>(callbackfn: (this: Z, value: T, index: number, array: T[]) => boolean, thisArg: Z): boolean;\r\n pure forEach(callbackfn: (this: void, value: T, index: number, array: T[]) => void): void;\r\n pure forEach(callbackfn: (this: void, value: T, index: number, array: T[]) => void, thisArg: undefined): void;\r\n pure forEach\u003cZ>(callbackfn: (this: Z, value: T, index: number, array: T[]) => void, thisArg: Z): void;\r\n pure map\u003cU>(this: [T, T, T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U): [U, U, U, U, U];\r\n pure map\u003cU>(this: [T, T, T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): [U, U, U, U, U];\r\n pure map\u003cZ, U>(this: [T, T, T, T, T], callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): [U, U, U, U, U];\r\n pure map\u003cU>(this: [T, T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U): [U, U, U, U];\r\n pure map\u003cU>(this: [T, T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): [U, U, U, U];\r\n pure map\u003cZ, U>(this: [T, T, T, T], callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): [U, U, U, U];\r\n pure map\u003cU>(this: [T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U): [U, U, U];\r\n pure map\u003cU>(this: [T, T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): [U, U, U];\r\n pure map\u003cZ, U>(this: [T, T, T], callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): [U, U, U];\r\n pure map\u003cU>(this: [T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U): [U, U];\r\n pure map\u003cU>(this: [T, T], callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): [U, U];\r\n pure map\u003cZ, U>(this: [T, T], callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): [U, U];\r\n pure map\u003cU>(callbackfn: (this: void, value: T, index: number, array: T[]) => U): U[];\r\n pure map\u003cU>(callbackfn: (this: void, value: T, index: number, array: T[]) => U, thisArg: undefined): U[];\r\n pure map\u003cZ, U>(callbackfn: (this: Z, value: T, index: number, array: T[]) => U, thisArg: Z): U[];\r\n pure filter(callbackfn: (this: void, value: T, index: number, array: T[]) => any): T[];\r\n pure filter(callbackfn: (this: void, value: T, index: number, array: T[]) => any, thisArg: undefined): T[];\r\n pure filter\u003cZ>(callbackfn: (this: Z, value: T, index: number, array: T[]) => any, thisArg: Z): T[];\r\n pure reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;\r\n pure reduce\u003cU>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;\r\n pure reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;\r\n pure reduceRight\u003cU>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;\r\n\r\n push(...items: T[]): number;\r\n pop(): T | undefined;\r\n reverse(): T[];\r\n shift(): T | undefined;\r\n slice(start?: number, end?: number): T[];\r\n sort(compareFn?: (a: T, b: T) => number): this;\r\n splice(start: number, deleteCount?: number): T[];\r\n splice(start: number, deleteCount: number, ...items: T[]): T[];\r\n unshift(...items: T[]): number;\r\n\r\n [n: number]: T;\r\n}\r\n```","author":{"url":"https://github.com/bradzacher","@type":"Person","name":"bradzacher"},"datePublished":"2017-07-14T01:24:16Z","url":"https://github.com/microsoft/TypeScript/issues/17181"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4388f6e1-732d-abe3-e523-1e4bb028de64 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E5A2:150E61:A12C9:DE522:6AFBC89B |
| html-safe-nonce | 78aa875d8bc0284eb61b7ccd2eb4652e6cdbb2718a8d728bbccfe1d2a3ba5e2c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNUEyOjE1MEU2MTpBMTJDOTpERTUyMjo2QUZCQzg5QiIsInZpc2l0b3JfaWQiOiI3NTAwMjIxMTUyMjIwNjYxOTE1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c33522980d5fed078770b9ffb83631cdd2a58852dc17c825a0df1bc6d59de75b |
| hovercard-subject-tag | issue:242872595 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/17181/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8f8ce2b433378f1ddc23e51318089473dff1e1f054bac210ee6993b7cb0414fa/microsoft/TypeScript/issues/17181 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8f8ce2b433378f1ddc23e51318089473dff1e1f054bac210ee6993b7cb0414fa/microsoft/TypeScript/issues/17181 |
| og:image:alt | The aim of this proposal is to add some immutability and pure checking into the typescript compiler. The proposal adds two new keywords that would give developers a means to define functions that a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bradzacher |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width