|
| 1 | +{ |
| 2 | + "action": "opened", |
| 3 | + "issue": { |
| 4 | + "url": "https://api.github.com/repos/microsoft/TypeScript/issues/35451", |
| 5 | + "repository_url": "https://api.github.com/repos/microsoft/TypeScript", |
| 6 | + "labels_url": "https://api.github.com/repos/microsoft/TypeScript/issues/35451/labels{/name}", |
| 7 | + "comments_url": "https://api.github.com/repos/microsoft/TypeScript/issues/35451/comments", |
| 8 | + "events_url": "https://api.github.com/repos/microsoft/TypeScript/issues/35451/events", |
| 9 | + "html_url": "https://github.com/microsoft/TypeScript/issues/35451", |
| 10 | + "id": 531531204, |
| 11 | + "node_id": "MDU6SXNzdWU1MzE1MzEyMDQ=", |
| 12 | + "number": 35451, |
| 13 | + "title": "Explicit `this: this` annotation necessary to catch unbound class method errors with `--no-implicit-this`", |
| 14 | + "user": { |
| 15 | + "login": "Retsam", |
| 16 | + "id": 2281166, |
| 17 | + "node_id": "MDQ6VXNlcjIyODExNjY=", |
| 18 | + "avatar_url": "https://avatars0.githubusercontent.com/u/2281166?v=4", |
| 19 | + "gravatar_id": "", |
| 20 | + "url": "https://api.github.com/users/Retsam", |
| 21 | + "html_url": "https://github.com/Retsam", |
| 22 | + "followers_url": "https://api.github.com/users/Retsam/followers", |
| 23 | + "following_url": "https://api.github.com/users/Retsam/following{/other_user}", |
| 24 | + "gists_url": "https://api.github.com/users/Retsam/gists{/gist_id}", |
| 25 | + "starred_url": "https://api.github.com/users/Retsam/starred{/owner}{/repo}", |
| 26 | + "subscriptions_url": "https://api.github.com/users/Retsam/subscriptions", |
| 27 | + "organizations_url": "https://api.github.com/users/Retsam/orgs", |
| 28 | + "repos_url": "https://api.github.com/users/Retsam/repos", |
| 29 | + "events_url": "https://api.github.com/users/Retsam/events{/privacy}", |
| 30 | + "received_events_url": "https://api.github.com/users/Retsam/received_events", |
| 31 | + "type": "User", |
| 32 | + "site_admin": false |
| 33 | + }, |
| 34 | + "labels": [ |
| 35 | + |
| 36 | + ], |
| 37 | + "state": "open", |
| 38 | + "locked": false, |
| 39 | + "assignee": null, |
| 40 | + "assignees": [ |
| 41 | + |
| 42 | + ], |
| 43 | + "milestone": null, |
| 44 | + "comments": 0, |
| 45 | + "created_at": "2019-12-02T20:51:16Z", |
| 46 | + "updated_at": "2019-12-02T20:51:16Z", |
| 47 | + "closed_at": null, |
| 48 | + "author_association": "NONE", |
| 49 | + "body": "**TypeScript Version:** 3.7.2\r\n\r\n<!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** `this` binding, `--no-implicit-this`, methods, `this: this`\r\n\r\n**Expected behavior:** The unbound `this` error is raised in both cases.\r\n\r\n**Actual behavior:** The compiler only catches the error in the case where the method was annotated as `this: this`.\r\n\r\n<!-- Did you find other bugs that looked similar? -->\r\n**Related Issues:** Found *many* `this` issues, couldn't find any related to the default inferred value in the absence of a `this` annotation on class methods. Sorry if I've just missed it.\r\n\r\n**Code**\r\n```ts\r\nclass Example1 {\r\n value = \"value\";\r\n method() {\r\n return this.value.toUpperCase();\r\n }\r\n}\r\n// Runtime-error, no compiler error\r\nnew Example1().method.call(null);\r\n\r\nclass Example2 {\r\n value = \"value\";\r\n method(this: this) {\r\n return this.value.toUpperCase();\r\n }\r\n}\r\n// Compiler error, as expected\r\nnew Example2().method.call(null);\r\n```\r\n\r\n\r\n\r\n<details><summary><b>Output<b></summary>\r\n\r\n```ts\r\n\"use strict\";\r\nclass Example1 {\r\n constructor() {\r\n this.value = \"value\";\r\n }\r\n method() {\r\n return this.value.toUpperCase();\r\n }\r\n}\r\n// Runtime-error, no compiler error\r\nnew Example1().method.call(null);\r\nclass Example2 {\r\n constructor() {\r\n this.value = \"value\";\r\n }\r\n method() {\r\n return this.value.toUpperCase();\r\n }\r\n}\r\n// Compiler error, as expected\r\nnew Example2().method.call(null);\r\n\r\n```\r\n\r\n\r\n</details>\r\n\r\n\r\n<details><summary><b>Compiler Options<b></summary>\r\n\r\n```json\r\n{\r\n \"compilerOptions\": {\r\n \"noImplicitAny\": true,\r\n \"strictNullChecks\": true,\r\n \"strictFunctionTypes\": true,\r\n \"strictPropertyInitialization\": true,\r\n \"strictBindCallApply\": true,\r\n \"noImplicitThis\": true,\r\n \"noImplicitReturns\": true,\r\n \"useDefineForClassFields\": false,\r\n \"alwaysStrict\": true,\r\n \"allowUnreachableCode\": false,\r\n \"allowUnusedLabels\": false,\r\n \"downlevelIteration\": false,\r\n \"noEmitHelpers\": false,\r\n \"noLib\": false,\r\n \"noStrictGenericChecks\": false,\r\n \"noUnusedLocals\": false,\r\n \"noUnusedParameters\": false,\r\n \"esModuleInterop\": true,\r\n \"preserveConstEnums\": false,\r\n \"removeComments\": false,\r\n \"skipLibCheck\": false,\r\n \"checkJs\": false,\r\n \"allowJs\": false,\r\n \"declaration\": true,\r\n \"experimentalDecorators\": false,\r\n \"emitDecoratorMetadata\": false,\r\n \"target\": \"ES2017\",\r\n \"module\": \"ESNext\"\r\n }\r\n}\r\n```\r\n\r\n\r\n</details>\r\n\r\n**Playground Link:** [Provided](https://www.typescriptlang.org/play/?ssl=4&ssc=1&pln=5&pc=1#code/MYGwhgzhAECiAeYC2AHEBTAjNA3gKGkOgDcwQBXdaAXmgCJSL06BuAopdAFwAsB7ACYAKAJS52RIgCdu5KQDtovAJYQAdI0pqufAKooU6KQGFI6UW0kBfPDYD0d6ACVy8rss4BaI1L5SANNDyfNDAfKjKGFLQPn548ugA7nCIqBiYomqcvIJqwGQgQvLkICAibHigkDAIyGjoAEzikppUtAxklKwS0Nn8wioQAFxKPKpi+JKSMlxyioManejaegZGphDm5T029o7G4SiRRjFSvgHQkDHwhsBc6ALxSSl1GA2Zfbn5pUUlZSxAA)\r\n \r\n--- \r\n\r\nGiven this behavior, [I've submitted a PR to typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/pull/1279) adding a linter rule that requires these annotations on all (non-arrow function) class methods that use `this`.\r\n\r\nHowever, I was wondering if there was a way this could be resolved on the Typescript compiler's side. The two code examples above seem semantically identical to me, and it's strange that they type-check differently. Is there a reason why it'd be harmful for `this: this` to be inferred from class methods that lack an explicit type annotation? \r\n\r\nNaturally, it would have to be behind a flag, but this is one of the few areas where I've hit runtime errors that the TS compiler hasn't caught. It's led to our codebase being somewhat paranoid about attaching methods to the prototype, preferring to define almost all methods as arrow function instance properties, which is a bit unfortunate from a memory perspective." |
| 50 | + }, |
| 51 | + "repository": { |
| 52 | + "id": 20929025, |
| 53 | + "node_id": "MDEwOlJlcG9zaXRvcnkyMDkyOTAyNQ==", |
| 54 | + "name": "TypeScript", |
| 55 | + "full_name": "microsoft/TypeScript", |
| 56 | + "private": false, |
| 57 | + "owner": { |
| 58 | + "login": "microsoft", |
| 59 | + "id": 6154722, |
| 60 | + "node_id": "MDEyOk9yZ2FuaXphdGlvbjYxNTQ3MjI=", |
| 61 | + "avatar_url": "https://avatars2.githubusercontent.com/u/6154722?v=4", |
| 62 | + "gravatar_id": "", |
| 63 | + "url": "https://api.github.com/users/microsoft", |
| 64 | + "html_url": "https://github.com/microsoft", |
| 65 | + "followers_url": "https://api.github.com/users/microsoft/followers", |
| 66 | + "following_url": "https://api.github.com/users/microsoft/following{/other_user}", |
| 67 | + "gists_url": "https://api.github.com/users/microsoft/gists{/gist_id}", |
| 68 | + "starred_url": "https://api.github.com/users/microsoft/starred{/owner}{/repo}", |
| 69 | + "subscriptions_url": "https://api.github.com/users/microsoft/subscriptions", |
| 70 | + "organizations_url": "https://api.github.com/users/microsoft/orgs", |
| 71 | + "repos_url": "https://api.github.com/users/microsoft/repos", |
| 72 | + "events_url": "https://api.github.com/users/microsoft/events{/privacy}", |
| 73 | + "received_events_url": "https://api.github.com/users/microsoft/received_events", |
| 74 | + "type": "Organization", |
| 75 | + "site_admin": false |
| 76 | + }, |
| 77 | + "html_url": "https://github.com/microsoft/TypeScript", |
| 78 | + "description": "TypeScript is a superset of JavaScript that compiles to clean JavaScript output.", |
| 79 | + "fork": false, |
| 80 | + "url": "https://api.github.com/repos/microsoft/TypeScript", |
| 81 | + "forks_url": "https://api.github.com/repos/microsoft/TypeScript/forks", |
| 82 | + "keys_url": "https://api.github.com/repos/microsoft/TypeScript/keys{/key_id}", |
| 83 | + "collaborators_url": "https://api.github.com/repos/microsoft/TypeScript/collaborators{/collaborator}", |
| 84 | + "teams_url": "https://api.github.com/repos/microsoft/TypeScript/teams", |
| 85 | + "hooks_url": "https://api.github.com/repos/microsoft/TypeScript/hooks", |
| 86 | + "issue_events_url": "https://api.github.com/repos/microsoft/TypeScript/issues/events{/number}", |
| 87 | + "events_url": "https://api.github.com/repos/microsoft/TypeScript/events", |
| 88 | + "assignees_url": "https://api.github.com/repos/microsoft/TypeScript/assignees{/user}", |
| 89 | + "branches_url": "https://api.github.com/repos/microsoft/TypeScript/branches{/branch}", |
| 90 | + "tags_url": "https://api.github.com/repos/microsoft/TypeScript/tags", |
| 91 | + "blobs_url": "https://api.github.com/repos/microsoft/TypeScript/git/blobs{/sha}", |
| 92 | + "git_tags_url": "https://api.github.com/repos/microsoft/TypeScript/git/tags{/sha}", |
| 93 | + "git_refs_url": "https://api.github.com/repos/microsoft/TypeScript/git/refs{/sha}", |
| 94 | + "trees_url": "https://api.github.com/repos/microsoft/TypeScript/git/trees{/sha}", |
| 95 | + "statuses_url": "https://api.github.com/repos/microsoft/TypeScript/statuses/{sha}", |
| 96 | + "languages_url": "https://api.github.com/repos/microsoft/TypeScript/languages", |
| 97 | + "stargazers_url": "https://api.github.com/repos/microsoft/TypeScript/stargazers", |
| 98 | + "contributors_url": "https://api.github.com/repos/microsoft/TypeScript/contributors", |
| 99 | + "subscribers_url": "https://api.github.com/repos/microsoft/TypeScript/subscribers", |
| 100 | + "subscription_url": "https://api.github.com/repos/microsoft/TypeScript/subscription", |
| 101 | + "commits_url": "https://api.github.com/repos/microsoft/TypeScript/commits{/sha}", |
| 102 | + "git_commits_url": "https://api.github.com/repos/microsoft/TypeScript/git/commits{/sha}", |
| 103 | + "comments_url": "https://api.github.com/repos/microsoft/TypeScript/comments{/number}", |
| 104 | + "issue_comment_url": "https://api.github.com/repos/microsoft/TypeScript/issues/comments{/number}", |
| 105 | + "contents_url": "https://api.github.com/repos/microsoft/TypeScript/contents/{+path}", |
| 106 | + "compare_url": "https://api.github.com/repos/microsoft/TypeScript/compare/{base}...{head}", |
| 107 | + "merges_url": "https://api.github.com/repos/microsoft/TypeScript/merges", |
| 108 | + "archive_url": "https://api.github.com/repos/microsoft/TypeScript/{archive_format}{/ref}", |
| 109 | + "downloads_url": "https://api.github.com/repos/microsoft/TypeScript/downloads", |
| 110 | + "issues_url": "https://api.github.com/repos/microsoft/TypeScript/issues{/number}", |
| 111 | + "pulls_url": "https://api.github.com/repos/microsoft/TypeScript/pulls{/number}", |
| 112 | + "milestones_url": "https://api.github.com/repos/microsoft/TypeScript/milestones{/number}", |
| 113 | + "notifications_url": "https://api.github.com/repos/microsoft/TypeScript/notifications{?since,all,participating}", |
| 114 | + "labels_url": "https://api.github.com/repos/microsoft/TypeScript/labels{/name}", |
| 115 | + "releases_url": "https://api.github.com/repos/microsoft/TypeScript/releases{/id}", |
| 116 | + "deployments_url": "https://api.github.com/repos/microsoft/TypeScript/deployments", |
| 117 | + "created_at": "2014-06-17T15:28:39Z", |
| 118 | + "updated_at": "2019-12-02T20:35:43Z", |
| 119 | + "pushed_at": "2019-12-02T20:46:21Z", |
| 120 | + "git_url": "git://github.com/microsoft/TypeScript.git", |
| 121 | + "ssh_url": "[email protected]:microsoft/TypeScript.git", |
| 122 | + "clone_url": "https://github.com/microsoft/TypeScript.git", |
| 123 | + "svn_url": "https://github.com/microsoft/TypeScript", |
| 124 | + "homepage": "https://www.typescriptlang.org", |
| 125 | + "size": 1194161, |
| 126 | + "stargazers_count": 56143, |
| 127 | + "watchers_count": 56143, |
| 128 | + "language": "TypeScript", |
| 129 | + "has_issues": true, |
| 130 | + "has_projects": true, |
| 131 | + "has_downloads": true, |
| 132 | + "has_wiki": true, |
| 133 | + "has_pages": false, |
| 134 | + "forks_count": 7644, |
| 135 | + "mirror_url": null, |
| 136 | + "archived": false, |
| 137 | + "disabled": false, |
| 138 | + "open_issues_count": 4099, |
| 139 | + "license": { |
| 140 | + "key": "apache-2.0", |
| 141 | + "name": "Apache License 2.0", |
| 142 | + "spdx_id": "Apache-2.0", |
| 143 | + "url": "https://api.github.com/licenses/apache-2.0", |
| 144 | + "node_id": "MDc6TGljZW5zZTI=" |
| 145 | + }, |
| 146 | + "forks": 7644, |
| 147 | + "open_issues": 4099, |
| 148 | + "watchers": 56143, |
| 149 | + "default_branch": "master" |
| 150 | + }, |
| 151 | + "organization": { |
| 152 | + "login": "microsoft", |
| 153 | + "id": 6154722, |
| 154 | + "node_id": "MDEyOk9yZ2FuaXphdGlvbjYxNTQ3MjI=", |
| 155 | + "url": "https://api.github.com/orgs/microsoft", |
| 156 | + "repos_url": "https://api.github.com/orgs/microsoft/repos", |
| 157 | + "events_url": "https://api.github.com/orgs/microsoft/events", |
| 158 | + "hooks_url": "https://api.github.com/orgs/microsoft/hooks", |
| 159 | + "issues_url": "https://api.github.com/orgs/microsoft/issues", |
| 160 | + "members_url": "https://api.github.com/orgs/microsoft/members{/member}", |
| 161 | + "public_members_url": "https://api.github.com/orgs/microsoft/public_members{/member}", |
| 162 | + "avatar_url": "https://avatars2.githubusercontent.com/u/6154722?v=4", |
| 163 | + "description": "Open source, from Microsoft with love" |
| 164 | + }, |
| 165 | + "sender": { |
| 166 | + "login": "Retsam", |
| 167 | + "id": 2281166, |
| 168 | + "node_id": "MDQ6VXNlcjIyODExNjY=", |
| 169 | + "avatar_url": "https://avatars0.githubusercontent.com/u/2281166?v=4", |
| 170 | + "gravatar_id": "", |
| 171 | + "url": "https://api.github.com/users/Retsam", |
| 172 | + "html_url": "https://github.com/Retsam", |
| 173 | + "followers_url": "https://api.github.com/users/Retsam/followers", |
| 174 | + "following_url": "https://api.github.com/users/Retsam/following{/other_user}", |
| 175 | + "gists_url": "https://api.github.com/users/Retsam/gists{/gist_id}", |
| 176 | + "starred_url": "https://api.github.com/users/Retsam/starred{/owner}{/repo}", |
| 177 | + "subscriptions_url": "https://api.github.com/users/Retsam/subscriptions", |
| 178 | + "organizations_url": "https://api.github.com/users/Retsam/orgs", |
| 179 | + "repos_url": "https://api.github.com/users/Retsam/repos", |
| 180 | + "events_url": "https://api.github.com/users/Retsam/events{/privacy}", |
| 181 | + "received_events_url": "https://api.github.com/users/Retsam/received_events", |
| 182 | + "type": "User", |
| 183 | + "site_admin": false |
| 184 | + } |
| 185 | +} |
0 commit comments