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

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

Open
mattmccutchen opened this issue Sep 24, 2018 · 12 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@mattmccutchen
Copy link
Contributor

mattmccutchen commented Sep 24, 2018

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

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Sep 24, 2018
@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added this to the Community milestone Sep 24, 2018
@planeight
Copy link

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

@hueyhe
Copy link

hueyhe commented Jan 18, 2019

Same issue here, any help?

@cmdcolin
Copy link

cmdcolin commented 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

@FireStormHR
Copy link

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.

@cmdcolin
Copy link

cmdcolin commented Jan 23, 2020

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.

@qwabra
Copy link

qwabra commented Feb 1, 2020

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

@andreyluiz
Copy link

In my case, the errors were off when I renamed a React component file from .ts to .tsx extension. TS seems to really care about the extension when it is a React component.

@Christopher-Hayes
Copy link

Christopher-Hayes commented Jan 6, 2021

In my case just removing .ts entirely fixed the error strangely enough. I thought omitting the extension didn't matter, but I guess sometimes it does.
For example:
import { YourType } from '@/path/to/file.ts' <-- before
import { YourType } from '@/path/to/file' <-- after

@kulkarnipradnyas
Copy link

create a tsconfig.json and "compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"sourceMap": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports":true
},
allowSyntheticDefaultImports particular thing will solve your problem

@bombillazo
Copy link

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.

This did it for me!

@denkhis
Copy link

denkhis commented Apr 22, 2022

In my case just removing .ts entirely fixed the error strangely enough. I thought omitting the extension didn't matter, but I guess sometimes it does. For example: import { YourType } from '@/path/to/file.ts' <-- before import { YourType } from '@/path/to/file' <-- after

thanks!
it worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests