Support shorthand ambient module declarations and wildcard chars in module names
follow up on https://github.com/Microsoft/TypeScript/issues/6614
Related issues: https://github.com/Microsoft/TypeScript/issues/247#issuecomment-173649104, https://github.com/Microsoft/TypeScript/issues/5787, https://github.com/Microsoft/TypeScript/issues/2709, https://github.com/Microsoft/TypeScript/issues/6371#issuecomment-169597919
Problems:
- Can not easily specify a loader extension for json or css (e.g. `json!a.json); currently every resources needs to have its own declaration
- Problem with migrating to .ts, with modules that do not have typings, and users just want to get by
Proposal:
-
Allow for short hand module declarations:
declare module "foo";to be equivalent to:
declare module "foo" { var _temp: any; export = _temp; } -
Allow module names to have a wildcard character and match that in lookup
declare module "json!*" { let json: any; export default json; } import d from "json!a/b/bar.json"; // lookup: // json!a/b/bar.json // json!* -
Additional the module "*" would allow for matching all unknown modules in the system, which can be used as a way to suppress all missing module errors
declare module "*"; import d from "some\unknown\module"; // no error, d is any -
Report no-implicit-any errors for modules declared using the shorthand notion Open issue: is this reported on declaration or use sites
Epic solution for #2709 !
Nice to see this is being worked on -- looks like an interesting solution that neatly captures all the outstanding requests.
Personally, I'd also like to see a compiler flag for the declare module "*"; use case -- it seems a little more natural, something like noExplicitAny, except for modules.
Thanks again for all the great work!
+1 Great solution! Looking forward to be able to delete all modules I created manually to keep the compiler happy
Allow for short hand module declarations:
declare module "foo";to be equivalent to:
declare module "foo" { var _temp: any; export = _temp; }
I think that this doesn't cover all cases, as this fails currently:
declare module "foo" {
var _tmp: any;
export = _tmp;
}
declare module "bar" {
import { x } from "foo"; // Error: Module foo has no exported member x
import y from "foo"; // Error: Module foo has no default export
}
thanks @ivogabe. the idea is you can use this module in any way you want and you just get any with no error. So the proposal should be updated to:
declare module "foo" {
var _temp: any;
export = _temp;
export default _temp;
}
I like the idea, but I think 2.0 is too far for this. This can help to solve so many issues the community has with lack of typings/etc.
Thanks, this certainly address my use cases!
@amcdnl, 2.0 is the next release after what is currently being stabilised as feature complete.
This is great! (GitHub really needs an upvote button.)
Two questions:
- Are the current proposals able to cover the following scenario?
import "xyz.less";
Can I do this?
declare module "xyz.less";
- Will this syntax be one of the proposals?
import template: string from "template.html";
@jack4it if I understand the proposal correct:
declare module "xyz.less";
Would automagically make the import xyz from 'xyz.less' or import * as xyz from 'xyz.less' as type any. If you are just importing for side effects, like in your first statement, it is entirely up to what module loader you are using and how it is configured. Almost any module loader will require some sort of plugin (though obviously using NodeJS's registry can absolve people from having to consider this, but that doesn't make for very "portable" code).
If you wanted the last to work, you would want to declare an ambient module like this:
declare module '*.html' {
const template: string;
export default template;
}
And you module loader would have to ensure that it process things appropriately.
Personally, I think any more automagic behaviour would be dangerous.
Will this syntax be one of the proposals? import template: string from "template.html";
No. 1. is it makes it much harder to update your definitions later on as now you have sprinkled template: string throughout your code base, and 2. type annotations will need to be supported on all import forms not only default imports import d from "mod" but for namespace, and property imports, and that would add yet another variant to the import syntax, and we already have plenty.
Interesting. #3691 is also related.
I'm a little bit surprised that you're planning to support declare module "*";, after your comment here about option 2. =)
Now I'm not sure whether to use that or to keep an explicit list of shorthand module declarations. Hmm.
Edit: After reading your comments in #247, I think that I'll go with the latter option, so that if I mistype a module name, it will be reported as missing as opposed to just being typed as any.
For those who seek for quick solution until 2.0 will be released: I've added line
if (moduleName.indexOf('!')>=0) return undefined;
just after
var resolvedModule = ts.getResolvedModule(ts.getSourceFileOfNode(location), moduleReferenceLiteral.text);
in resolveExternalModuleNameWorker function, so TS will ignore all plugin's imports.
I know it is quick-n-dirty solution, but now it allows me to perform syntax check of TS code when using SystemJS plugins.
Does the proposed solution also allow matching of modules with rooted and relative names?
I ask because all the examples given thus far only demonstrate the non-relative case.
In other words, will this:
// FILE: ambient.d.ts
declare module '*.css' {
var r: string;
export default r;
}
// FILE: something.ts
/// <reference path="ambient.d.ts" />
import css from './component.css';
work as expected despite the leading ./?
@mark-buer, no the current proposal does not handle that. can you elaborate on your scenario?
@mhegazy I would expect example of @mark-buer would work, because pattern '*.css' matches './component.css'.
As use-case: I am using SystemJS with TS. And I have a lot of resources loaded with SystemJS plugin in the way: import template from './component.html!text' that is valid SystemJS import.
The relative path is required, because include is done from folder, where TS-code of component is located. It would be very strange to import css/template of component using absolute paths.
@mhegazy, @olostan summed it up nicely.
Currently I'm using the same setup with SystemJS like @olostan and used a variation of his fix from here to allow using SystemJS imports without getting tsc errors: https://github.com/olee/TypeScript/commit/f55e5e7a042512d0a099b64cbb5d127d5b57a161
I think the proposed solution at the top of this issue would greatly help to solve such issues. Has there been any update / official statement on this yet?
Same use case here, using Webpack and would like to import HTML files via import template from './template.html'; At the moment I can work around it by using require() syntax, but that's inconsistent with the rest of the code style (and I would actually prefer forbidding require() in tslint). Also, it makes it difficult to transition from Babel since Babel has no problem with this syntax. IMHO the ability to import files other than ".ts" is essential to integration with modern build tools and can't come soon enough.
I second what @mischkl said. The ability to easily load templates as string values from .html files (or .twig, .handlebars, or any other arbitrary file extension), and have them compile along with TypeScript files as an integral part of the source code is essential.
This is one of the main requirements I have that would need to be met before I would decide to use TypeScript in a large web application. Solving this in a hassle-free way would truly set TypeScript apart from the competition.