Imports in .d.ts files break wildcard modules declarations
See original GitHub issueTypeScript 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:
- Created 5 years ago
- Reactions:15
- Comments:12 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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: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โ?