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

Object literal not recognized as namespace #26505

Closed
texastoland opened this issue Aug 17, 2018 · 2 comments
Closed

Object literal not recognized as namespace #26505

texastoland opened this issue Aug 17, 2018 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@texastoland
Copy link
Contributor

texastoland commented Aug 17, 2018

TypeScript Version: 3.1.0xxxxx

Search Terms:

namespace empty object literal and https://github.com/Microsoft/TypeScript/tree/master/tests/cases

Code

var ns = {};     // recognized as a declaration for a namespace `ns`
ns.constant = 1; // recognized as a declaration for var `constant`
// ^^^^^^^^ <- Property 'constant' does not exist on type '{}'.

Expected behavior:

TypeScript 2.8 adds support for understanding more namespace patterns in .js files. Empty object literals declarations on top level, just like functions and classes, are now recognized as as namespace declarations in JavaScript.

Release notes (source)

Actual behavior:

Compile error.

Playground Link:

Playground

Related Issues:

I'm migrating a codebase and tried a few minor variations unsuccessfully.

@texastoland
Copy link
Contributor Author

texastoland commented Aug 17, 2018

patterns in .js files

Ugh I just realized I converted my file to .ts. Is this feature specific to @ts-check?

@AlCalzone
Copy link
Contributor

Yes, this is specific to JavaScript. In TypeScript you should declare the intended type of your variables beforehand. One exception will come for function types:
#26368

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants