-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Root scope type #10050
Comments
Workaround - manual add all defined classes/functions/variables to Window interface. class XXX {
static say() { alert( 'Good!' ) }
}
var win : Window = this
interface Window {
XXX : typeof XXX
}
win.XXX.say() |
Suggestion: automatic register all global entities in class XXX {}
function YYY { return ZZZ }
var ZZZ = this // "this" has type "Global"
var xxx = new (win.ZZZ.ZZZ.YYY().XXX) // Returns "XXX" type |
Why i need this For mocking. function myAlert( message : string ) {
alert( message )
}
class XXX {
$ = window
run( ) {
this.$.myAlert( 'hello' )
}
} var alertedMessage
var xxx = new XXX
xxx.$ = Object.assign( Object.create( this ) , {
myAlert : message => alertedMessage = message
} )
xxx.run()
console.assert( alertedMessage === 'hello' ) For overriding classes in runtime component tree. |
I agree that globals are more painful to model than non-globals. Both So there is, for example, both I have a project where scripts can be run in a sandbox, and I'd like to tell TypeScript to type-check those scripts using the definitions in, say, Suppose there was a way to tell the compiler "this declaration's members are also globals", something like |
This has been an intentional design choice for TypeScript. global pollution, though common, are not considered to be a good practice. the global object is different based on the engine/host implementation, e.g. window, self, global, etc.. even the value of |
This is normal practice. Tools must support this and should not impose any doctrine.
Yes, this is a problem. Workaround:
|
Will the situation change when |
TypeScript does support this. first |
TypeScript Version: 1.8.10
Code
Expected behavior:
No errors.
Actual behavior:
Error: Property 'XXX' does not exist on type 'Window'.
Code
Expected behavior:
Error: Property 'broken' does not exist on type 'XXX'.
Actual behavior:
No type checks.
The text was updated successfully, but these errors were encountered: