question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:14
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

53reactions
cmdcolincommented, Apr 17, 2019

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

36reactions
FireStormHRcommented, Jan 22, 2020

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 noImplicitAny turned 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix 'Cannot use namespace as a type ts(2709)' in ...
I was struggling to figure out how I could write a type definition for passing an external/3rd party module around.
Read more >
List of the armcc error and warning messages - Arm Developer
When the compiler is not in C99 mode, this error is produced by a function declaration f(V) where V is a void type....
Read more >
The starting point for learning TypeScript
Declaration Files. Learn how to write declaration files to describe existing JavaScript. Important for DefinitelyTyped contributions.
Read more >
Defining Your Own Python Function
This means that when you write code within a function, you can use variable names ... C:\Users\john\Documents\Python\doc>python foo.py Before calling f() ...
Read more >
Python Tutorial - Getting Started with Python and Python Basics
You do NOT need to declare variables (name and type) before using them. ... In Python, you need to include a dummy statement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found