microsoft / microsoft/TypeScript

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

Open
#27,311 12 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Error Messages Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TS2709 error in the linked TypeScript Playground using the dummy declare module "foo"; and nested declare module "bar" example. Trace the compiler's handling of the imported Foo in the type position, then verify that the result is either a sensible diagnostic or no error for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.