Skip to content
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

JS: properties on class and function declarations are not type checked #16239

Closed
OliverJAsh opened this issue Jun 3, 2017 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Jun 3, 2017

TypeScript Version: 2.3.4

Code

class Foo {}
Foo.bar = 1; // expected error, but got none

const Foo2 = () => {};
Foo2.bar = 1; // error

function FooBar() {}
FooBar.bar = 1; // expected error, but got none

This errors is TS files, but not in JS files where checkJs is enabled.

@OliverJAsh OliverJAsh changed the title JS: properties on class are not type checked JS: properties on class and function declarations are not type checked Jun 3, 2017
@OliverJAsh
Copy link
Contributor Author

OliverJAsh commented Jun 4, 2017

Interestingly, the expected/missing errors do appear when the example is wrapped in a block:

{
class Foo {}
Foo.bar = 1; // error

const Foo2 = () => {};
Foo2.bar = 1; // error

function FooBar() {}
FooBar.bar = 1; // error
}

@DanielRosenwasser
Copy link
Member

We're intentionally lax about this because there's no such thing as namespace merging (i.e. a function/class/namespace followed by a namespace) in JS. Tacking properties onto values is pretty common for JS users, so I believe we allow it for top-level declarations.

As a matter of fact, this is something we're considering in TS itself. Check out #15868.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 5, 2017
@DanielRosenwasser
Copy link
Member

Actually, the second part about only doing this for top-level declarations is not true - we should not issue an error even in the block scope. You'll see the error go away if you move the statements into a function body. @mhegazy mentioned that that should be a bug.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants