Typescript: Strange "Cannot use namespace 'Foo' as a type" error with dummy `declare module "foo";` declaration

Created on 24 Sep 2018  Β·  7Comments  Β·  Source: microsoft/TypeScript

Maybe this is a question, but it has come up multiple times on Stack Overflow (most recently here) without a good answer, so I'm asking it here.


TypeScript Version: master (03af107)


Search Terms: "cannot use namespace" "as a type" TS2709 "declare module"

Code

declare module "foo";

declare module "bar" {
    import { Foo } from "foo";
    let x: Foo;
}

Expected behavior: An error that makes sense, or no error?

Actual behavior: error TS2709: Cannot use namespace 'Foo' as a type.

Playground Link: link

Related Issues: None found

Bug help wanted

Most helpful comment

This error was happening to me when I accidentally had declare module "mymodule" but then actually was using import {MyInterface} from 'mymodule' and this caused any usage of MyInterface after that to result in the errors "Cannot use namespace 'MyInterface' as a type" and "Property 'myprop' of exported interface has or is using private name 'MyInterface'." on the same line

I had to delete the declare module "mymodule" line and then it worked. This was very confusing to me so I'd add a +1 for a better error message if possible

All 7 comments

It is technically correct from the perspective of how the compiler is implemented πŸ™ƒ

In the specific case where we imported an any-like value in an ambient context and then try to use it as a type, we could say something like:

The name 'Foo' does not refer to a known type

Open to bikeshedding

Im having the same issue with trying to use create-react-app with typescript ver 3.2.1 and loona

Same issue here, any help?

This error was happening to me when I accidentally had declare module "mymodule" but then actually was using import {MyInterface} from 'mymodule' and this caused any usage of MyInterface after that to result in the errors "Cannot use namespace 'MyInterface' as a type" and "Property 'myprop' of exported interface has or is using private name 'MyInterface'." on the same line

I had to delete the declare module "mymodule" line and then it worked. This was very confusing to me so I'd add a +1 for a better error message if possible

This error was happening to me when I accidentally had declare module "mymodule" but then actually was using import {MyInterface} from 'mymodule' and this caused any usage of MyInterface after that to result in the errors "Cannot use namespace 'MyInterface' as a type" and "Property 'myprop' of exported interface has or is using private name 'MyInterface'." on the same line

I had to delete the declare module "mymodule" line and then it worked. This was very confusing to me so I'd add a +1 for a better error message if possible

@cmdcolin having your module declared is necessary when using typescript and having noImplicitAnyturned on in the ts.config. To solve this namespace error while keeping your declaration you can put typeof in front of the place where you are using the namespace.

Edit: reading the OP more clearly, it makes more sense why this exists but I guess I just wanted to highlight that oftentimes this is in a separate declare.d.ts file so it's sort of hard to debug sometimes.

welcome TypeScript v3.8 with import type {} from ''

Was this page helpful?
0 / 5 - 0 ratings