Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cannot use typeof on an empty module #3053

Closed
NoelAbrahams opened this issue May 6, 2015 · 3 comments
Closed

Cannot use typeof on an empty module #3053

NoelAbrahams opened this issue May 6, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@NoelAbrahams
Copy link

Hi,

TS: 1.4/1.5

In the following, the compiler issues an error on the last line:

module foo {

    export module bar {
        export class F { }
    }

    export module empty {
    }
}

var x: typeof foo.bar; // okay
var y: typeof foo.empty; // Property empty does not exist on type 'typeof foo'

This has turned out to be a problem for the case when a module is defined across multiple files. For certain references the module will be empty, as it is initialised in a different context.

Why do we have no error for bar but an error for empty?

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 6, 2015
@RyanCavanaugh
Copy link
Member

This is the difference between an instantiated module (one that has at least one value, or executable code, in it) and an uninstantiated module (which only has types or other uninstantiated modules in it).

It's meaningless to say typeof empty because there is no value named empty. At the point that you get this error, typeof empty, if it existed, would be {}, so you should write that instead.

If you want to make empty instantiated for some workaround reason, you can place a var dummy; in it to force it to be instantiated.

This has turned out to be a problem for the case when a module is defined across multiple files

Can you clarify this? If the compiler can't see those other files, it's meaningless to write typeof empty; if it can, you shouldn't get this error.

@NoelAbrahams
Copy link
Author

@RyanCavanaugh,

The use case is to capture the shape of an existing module and to ensure that we get a compilation error if the shape of the original module changes:

// Original
module foo {
    export module empty {
    }
}

// Copy
interface Copy {
    foo: {
       empty: typeof foo.empty;
   }
}

At the point that you get this error, typeof empty, if it existed, would be {}, so you should write that instead

That's a rather strange argument. What if someone were to add a type to the empty module at a later stage? We'd have to go change all the references.

I took a look at #420, but wasn't certain that would cover the case. In any case to the naked eye this looks like a bug.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 10, 2015

looks like #420 would address the underlying issue.

@mhegazy mhegazy closed this as completed Aug 10, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants