{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/25628","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/25628/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/25628/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/25628/events","html_url":"https://github.com/Microsoft/TypeScript/issues/25628","id":340851254,"node_id":"MDU6SXNzdWUzNDA4NTEyNTQ=","number":25628,"title":"[proposal] [tc39] Static variables inside functions","user":{"login":"NeekSandhu","id":11316020,"node_id":"MDQ6VXNlcjExMzE2MDIw","avatar_url":"https://avatars3.githubusercontent.com/u/11316020?v=4","gravatar_id":"","url":"https://api.github.com/users/NeekSandhu","html_url":"https://github.com/NeekSandhu","followers_url":"https://api.github.com/users/NeekSandhu/followers","following_url":"https://api.github.com/users/NeekSandhu/following{/other_user}","gists_url":"https://api.github.com/users/NeekSandhu/gists{/gist_id}","starred_url":"https://api.github.com/users/NeekSandhu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NeekSandhu/subscriptions","organizations_url":"https://api.github.com/users/NeekSandhu/orgs","repos_url":"https://api.github.com/users/NeekSandhu/repos","events_url":"https://api.github.com/users/NeekSandhu/events{/privacy}","received_events_url":"https://api.github.com/users/NeekSandhu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-07-13T01:33:46Z","updated_at":"2018-07-13T02:56:49Z","closed_at":null,"author_association":"NONE","body":"## Search Terms\r\n\r\nfunction,static,variable\r\n\r\n## Suggestion\r\n\r\nI believe it'd be really useful to have static variables inside methods/functions, initialized once, reused for subsequent calls and memory lifetime same as containing scope i.e the function itself.\r\n\r\n> This issue is not a proposal to get this implemented into TypeScript as I am well aware that TS team won't implement stuff that is not standard/draft. Instead, this issue is a proof of concept and market validation attempt to see if the community would want something like that. And if so, we can move forward with proposing this to tc39 folks.\r\n\r\n## Use Cases\r\n\r\nAlmost every app has a function or method that needs to \"preserve\" its state across multiple calls, a counter for example. Current ways out of this situation are either IIFE's or making those variables top-level. Both of which are ugly. I think the same could be done much more neatly with `static` variables.\r\n\r\n## Examples\r\n\r\n```typescript\r\n// main.ts\r\n\r\nclass DBService {\r\n    static checkURI = (uri: string) => {\r\n    \t// this way pattern won't be (re)compiled every time checkURI is called\r\n        static const httpRE = /^https?/;\r\n\r\n        // relevant variables contained in relevant scopes\r\n        // historically counters are either top-level variables or props of the class\r\n        static let errCount = 0;\r\n\r\n        if (errCount > 10) {\r\n            throw new Error(`Some error`);\r\n        }\r\n\r\n        if (!httpRE.test(uri)) {\r\n            errCount++;\r\n            return false;\r\n        }\r\n    }\r\n}\r\n```\r\n`===`\r\n```javascript\r\n// compiled.js\r\n\r\nvar DBService = /** @class */ (function () {\r\n    function DBService() {\r\n    }\r\n    DBService.checkURI = (function () {\r\n        var httpRE = /^https?/;\r\n        var errCount = 0;\r\n        return function (uri) {\r\n            if (errCount > 10) {\r\n                throw new Error(\"Some error\");\r\n            }\r\n            if (!httpRE.test(uri)) {\r\n                errCount++;\r\n                return false;\r\n            }\r\n        };\r\n    })();\r\n    return DBService;\r\n}());\r\n\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [x] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [ ] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/404710851","html_url":"https://github.com/Microsoft/TypeScript/issues/25628#issuecomment-404710851","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/25628","id":404710851,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNDcxMDg1MQ==","user":{"login":"qti3e","id":13242052,"node_id":"MDQ6VXNlcjEzMjQyMDUy","avatar_url":"https://avatars1.githubusercontent.com/u/13242052?v=4","gravatar_id":"","url":"https://api.github.com/users/qti3e","html_url":"https://github.com/qti3e","followers_url":"https://api.github.com/users/qti3e/followers","following_url":"https://api.github.com/users/qti3e/following{/other_user}","gists_url":"https://api.github.com/users/qti3e/gists{/gist_id}","starred_url":"https://api.github.com/users/qti3e/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qti3e/subscriptions","organizations_url":"https://api.github.com/users/qti3e/orgs","repos_url":"https://api.github.com/users/qti3e/repos","events_url":"https://api.github.com/users/qti3e/events{/privacy}","received_events_url":"https://api.github.com/users/qti3e/received_events","type":"User","site_admin":false},"created_at":"2018-07-13T02:56:49Z","updated_at":"2018-07-13T02:56:49Z","author_association":"NONE","body":"I absolutely like this proposal;\r\n```js\r\nconst name_a = new ArrayBuffer(2000);\r\nfunction a() {\r\n  ...\r\n}\r\nconst name_b = new ArrayBuffer(5000);\r\nfunction b() {\r\n  ...\r\n}\r\n\r\n```\r\nbecomes:\r\n```ts\r\nfunction a() {\r\n  static const name = new ArrayBuffer(2000);\r\n}\r\nfunction b() {\r\n  static const name = new ArrayBuffer(5000); // I can use the same name here.\r\n}\r\n```"}}
{"actor":{"display_login":"qti3e"}}