-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Imports in .d.ts files break wildcard modules declarations #28097
Comments
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"? |
I've faced this issue before. It would really help for beginners writing their first modules having a good error message like the one you propose @weswigham. I'm happy to work on this if no one else is doing it already. |
I wonder if it could be an error to augment a module that does not exist, if the compilation (apparently) cannot use the augmented file? |
Helped me greatly.
|
Maybe offtopic but... I came across this thread when I was looking for a way to Someone recommended that I could use wildcard modules for this, but I don't think that is the case. This is how I fixed my issue: Before converting my project to typescript, I had this line: import imageUrls from "../../img/*.*"; I was kind of disappointed that this did no longer work. I replaced it with this: const imageUrls = require("../../img/*.*"); and then I installed {
"compilerOptions": {
"lib": ["es2017", "dom"],
"allowSyntheticDefaultImports": true
},
"typeRoots": ["./node_modules/@types", "./js/types"],
"include": ["js"],
"exclude": ["node_modules"]
} I hope this helps someone who might run into a similar issue. If there is a way to do this with a wildcard module I would love to know, though. I'm new to TypeScript. |
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 /**
* 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
} |
@weswigham sorry to ping this ancient thread. A coworker of mine just encountered this same issue, and I’ve personally helped quite a few people debug their d.ts files only to find that it’s this very issue. For people who are new to TypeScript it can be quite a frustrating pitfall. I see this issue is in the backlog—is there anything that needs to be done to boost its visibility/priority a bit? Some sort of warning here would really go a long way to help folks debug mysterious typing issues. |
It's tagged |
@weswigham rad, thank you 🙏 |
If someone stumbles on this thread from search, you can also use declare type MyType = {
myproperty: (param: import("example-library").ExampleType) => any;
}; See https://devblogs.microsoft.com/typescript/announcing-typescript-2-9-2/#import-types |
This is a huge pitfall and causes a lot of headaches, especially since benmosher's code above works (#28097 (comment)) but only because the module they're declaring isn't the same as the module being imported. Wanting to extend the same module doesn't work: // (as an example: converting a named export to a default export)
declare module '@x/y/z' {
import type { DataSource } from '@x/y/z'
export default DataSource
} The above will make any TS files who Somehow this doesn't happen when the import statement is outside/before the "declare module", but then you can't have other declarations in that file. It's...yeah. Lots of trial and error 😅 |
Holy cow that's true, I had been trying to debug why my .d.ts file wasn't picked up for over an hour, thanks :) |
Gently pinging - is there any plan to fix this in typescript 5.0? Thanks! |
TypeScript Version: 3.1.3
Search Terms:
import d.ts wildcard module
Code
main.ts
typings.d.ts
Compile with:
Expected behavior:
Compiled without errors.
Actual behavior:
Compiled with error
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:to workaround #10178
The text was updated successfully, but these errors were encountered: