Title: Constructor functions are not recognized as equivalent to classes · Issue #27328 · microsoft/TypeScript · GitHub
Open Graph Title: Constructor functions are not recognized as equivalent to classes · Issue #27328 · microsoft/TypeScript
X Title: Constructor functions are not recognized as equivalent to classes · Issue #27328 · microsoft/TypeScript
Description: TypeScript Version: 3.1.0-dev.20180922 Search Terms: js constructor function Code // clazz1c.js function clazz1c(){ this.constructorOnly = 0 // (1.) [ts] 'this' implicitly has type 'any' because it does not have a type annotation. this.c...
Open Graph Description: TypeScript Version: 3.1.0-dev.20180922 Search Terms: js constructor function Code // clazz1c.js function clazz1c(){ this.constructorOnly = 0 // (1.) [ts] 'this' implicitly has type 'any' because it...
X Description: TypeScript Version: 3.1.0-dev.20180922 Search Terms: js constructor function Code // clazz1c.js function clazz1c(){ this.constructorOnly = 0 // (1.) [ts] 'this' implicitly has type 'any...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27328
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constructor functions are not recognized as equivalent to classes","articleBody":"**TypeScript Version:** 3.1.0-dev.20180922\r\n\r\n**Search Terms:**\r\n\r\njs constructor function\r\n\r\n**Code**\r\n\r\n```js\r\n// clazz1c.js\r\nfunction clazz1c(){\r\n\tthis.constructorOnly = 0 // (1.) [ts] 'this' implicitly has type 'any' because it does not have a type annotation.\r\n\tthis.constructorUnknown = undefined // (1.) [ts] 'this' implicitly has type 'any' because it does not have a type annotation.\r\n}\r\n\r\nclazz1c.prototype.method = function() {\r\n\tthis.constructorOnly = 3\r\n\tthis.constructorUnknown = \"plunkbat\"\r\n}\r\n\r\nmodule.exports=clazz1c\r\n```\r\n\r\n```ts \r\n// node-global.ts\r\n/// \u003creference types=\"node\" />\r\nexport {}\r\nimport Clazz1cType = require(\"../clazz1c\") // (2.) (alias) function Clazz1cType(): void\r\nvar clazz1bGlobalVar: Clazz1bType // (2.) var clazz1bGlobalVar: any\r\ndeclare global {\r\n\tnamespace NodeJS {\r\n\t\tinterface Global {\r\n\t\t\tclazz1bGlobalVar: Clazz1cType // (4.) (property) NodeJS.Global.clazz1bGlobalVar: any\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n\r\n```js\r\n// index.js\r\n var clazz1c = require(\"./clazz1c\") // (3.) var clazz1c: typeof clazz1c\r\n var clazz1cInstance = new clazz1c(); // (3.) var clazz1bGlobalVar: any\r\nglobal.clazz1bGlobalVar=clazz1bInstance // (4.) (property) NodeJS.Global.clazz1bGlobalVar: any\r\n```\r\n\r\n```json\r\n// jsconfig.json\r\n{\r\n \"compilerOptions\": {\r\n\t\t\"allowSyntheticDefaultImports\": true,\r\n\t\t\"checkJs\": true,\r\n\t\t\"noEmit\": true,\r\n\t\t\"module\": \"commonjs\",\r\n\t\t\"moduleResolution\": \"node\",\r\n\t\t\"strict\": true,\r\n\t\t\"noImplicitAny\": true,\r\n\t\t\"noImplicitThis\": true,\r\n\t\t\"target\": \"es5\",\r\n \"baseUrl\": \".\",\r\n \"lib\": [\r\n \"es2015\",\r\n \"dom\"\r\n\t\t]\r\n\t},\r\n \"exclude\": [\r\n \"node_modules\"\r\n ],\r\n \"typeAcquisition\": {\r\n \"enable\": false\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n1. `this` inside the constructor is infered based on this-property assignments\r\n2. function is recognized as constructor function; assignment gives type of `class`?\r\n3. type equivalent to a class t.i. `(alias) class clazz1c` (per default type is exported too)\r\n4. typed equivalent to type of constructor function of clazz1c, sth like `new() => clazz1c`?\r\n\r\n**Actual behavior:**\r\n1. `this` inside the constructor is not infered based on this-property assignments\r\n2. function is not recognized as constructor function; assignment gives `any` type\r\n3. type is not recognized as equivalent to a class\r\n4. typed as any\r\n\r\n**Playground Link:** \r\nFor (1) see http://www.typescriptlang.org/play/#src=function%20clazz1c()%7B%0D%0A%09this.constructorOnly%20%3D%200%0D%0A%09this.constructorUnknown%20%3D%20undefined%0D%0A%7D%0D%0A%0D%0Aclazz1c.prototype.method%20%3D%20function()%20%7B%0D%0A%09this.constructorOnly%20%3D%203%0D%0A%09this.constructorUnknown%20%3D%20%22plunkbat%22%0D%0A%09%7D%0D%0A%0D%0Amodule.exports%3Dclazz1c\r\n\r\n(2), (3) and (4) not in playground link, but doable as github tag on request\r\n\r\n**Related Issues:**\r\nNone found\r\n","author":{"url":"https://github.com/joma74","@type":"Person","name":"joma74"},"datePublished":"2018-09-24T22:05:18Z","url":"https://github.com/microsoft/TypeScript/issues/27328"}
| 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:baf6d6cf-61c1-3e70-4d02-e4e948a8c2b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1C4:842EC:23DAC2E:2FCEB6D:6B019A3E |
| html-safe-nonce | 353a46bba5c036b8d1dd66c2b6cd673b9ad099d3ae8854ebc97215d024cf6e66 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUM0Ojg0MkVDOjIzREFDMkU6MkZDRUI2RDo2QjAxOUEzRSIsInZpc2l0b3JfaWQiOiI0NjU2OTU2MDQ5MjY0MzE5MDM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 57b2425322bba428ae86c4712c0d60d414001410bd776a216deef0b62ebfe32a |
| hovercard-subject-tag | issue:363337931 |
| 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/27328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4f3025f83ea650de6547be2e2af9d62b487247d8d9056874aa5a81c05b15067c/microsoft/TypeScript/issues/27328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4f3025f83ea650de6547be2e2af9d62b487247d8d9056874aa5a81c05b15067c/microsoft/TypeScript/issues/27328 |
| og:image:alt | TypeScript Version: 3.1.0-dev.20180922 Search Terms: js constructor function Code // clazz1c.js function clazz1c(){ this.constructorOnly = 0 // (1.) [ts] 'this' implicitly has type 'any' because it... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | joma74 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width