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.

Imports in .d.ts files break wildcard modules declarations

See original GitHub issue

TypeScript Version: 3.1.3

Search Terms:

import d.ts wildcard module

Code

main.ts

/// <reference path="./typings.d.ts" />

import template from './template.html';

typings.d.ts

import * as _angular from 'angular';

declare module '*.html' {
  const content : string;
  export default content;
}
{
  "name": "ts-bug",
  "version": "1.0.0",
  "dependencies": {
    "angular": "^1.7.5"
  }
}

Compile with:

tsc main.ts

Expected behavior:

Compiled without errors.

Actual behavior: Compiled with error

main.ts:3:22 - error TS2307: Cannot find module './template.html'.

3 import template from './template.html';
                       ~~~~~~~~~~~~~~~~~

Removing import * as _angular from 'angular'; fixes the issue.

Side Note 1

Regular module declarations work regardless of imports being present.

Side Note 2 import * as _angular from 'angular'; is needed to later do:

declare global {
  const angular : typeof _angular;
}

to workaround https://github.com/Microsoft/TypeScript/issues/10178

Issue Analytics

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

github_iconTop GitHub Comments

31reactions
benmoshercommented, May 6, 2019

Not sure if this helps others but I was just trying to import to get a type from a dependency, and stumbled into this clearer but not-obvious (to me anyway) alternative. Apparently you can import within the module declarations themselves and this compiles the same (AFAICT) as no-imports declarations. Not sure if this satisfies OPโ€™s desired outcome but this does what I was looking for:

/**
 * GraphQL query files.
 */
declare module "*.gql" {
  // import is tucked into (and scoped only within?) the module declaration
  import { gql } from 'graphql-tag'
  // currently `any` but could be well-defined in a future graphql-tag release
  const doc: ReturnType<typeof gql>
  export = doc
}
5reactions
weswighamcommented, Oct 28, 2018

An import makes a file a module, which consequently makes the module statement a module augmentation and not a module declaration. A module augmentation differs in that it only adds to existing modules in the compilation. Maybe we could issue a more useful error (or related span) here? Like โ€œa module augmentation exists which matches this import, however no module file or declaration doesโ€?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular/Typescript - Wildcard module declaration
I'm deleting my answer because I discovered it is completely wrong: wildcard declare module does work for relative imports in TypeScript. Iย ...
Read more >
Documentation - Modules .d.ts - TypeScript
The types which are exported can then be re-used by consumers of the modules using either import or import type in TypeScript code...
Read more >
Wildcard imports should not be used - SonarSource Rules
TypeScript static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your TYPESCRIPT code.
Read more >
The Definitive TypeScript 4.8 Guide - SitePen
A tsconfig.json file identifies a project to the TypeScript compiler ... In a .d.ts file, a wildcard module declaration describes how allย ...
Read more >
Modules โ€” Clang 16.0.0git documentation
Standard C++ Modules; Objective-C Import declaration; Includes as imports; Module maps ... Lexical structure; Module map file; Module declaration.
Read more >

github_iconTop Related Medium Post

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