-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor functions are not recognized as equivalent to classes #27328
Comments
I think this is the intended behavior under |
@andy-ms Quoting https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files#constructor-functions-are-equivalent-to-classes, i interpreted that said hold without further hinting or limitations?
If not, i could be fine with annotating the constructor function with the TS comment hint In any case, according to the title of this issue, i would like to discuss (2) (3) and (4) as not "working as intended". Neither the resulting TS signature nor the compiler's evaluated/exported type of such a constructor function are currently handled equivalent to a class. On any usage in said ts or js files, the constructor function is still interpreted as sth like P.S. If i rewrite clazz1c to be a proper es2015 class, the exact same usage in said ts or js files works as intended. So this issue aims to get |
See also #18171 relating to (1) |
That document is titled "Type Checking JavaScript Files" -- in JS files the rules are looser by default. But when you set |
@andy-ms Okay, got that. So it has to be // clazz1c.js
/**
* @constructor
*/
function clazz1c(){
this.constructorOnly = 0
this.constructorUnknown = undefined
}
module.exports=clazz1c to make // std-globals.ts
export {}
import Clazz1cType = require("../clazz1c") // (A)
// (C)
declare global {
var clazz1CGlobalVar: Clazz1cType // (B)
}
Even no hand-written declaration like // index.js
var clazz1cInstance = new clazz1c();
clazz1CGlobalVar=clazz1cInstance // (D) gives |
See also #27550.
Although other worded, but may apply here: As in
|
This should all work as expected now. @joma74 do you mind trying typescript@next to check? |
@sandersn Checked, but sorry can't see it fixed. For reference of testcases see joma74/ts-in-js-1 on branch master and branch ts-issue-global-demo. On both then run |
P.S. would expect noImplicitAny to error if it would be an error :)
|
This should be fixed now. |
TypeScript Version: 3.1.0-dev.20180922
Search Terms:
js constructor function
Code
Expected behavior:
this
inside the constructor is infered based on this-property assignmentsclass
?(alias) class clazz1c
(per default type is exported too)new() => clazz1c
?Actual behavior:
this
inside the constructor is not infered based on this-property assignmentsany
typePlayground Link:
For (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
(2), (3) and (4) not in playground link, but doable as github tag on request
Related Issues:
None found
The text was updated successfully, but these errors were encountered: