Description
Declaring ambient external modules
Related issues:
- Support looking for modules under node_modules when importing #247 (comment)
- Better support for loader plugins #5787
- Importing files other than TS modules #2709
- allow type 'unsafe' way to doing es6 style imports #6371 (comment)
Problems:
- Can not easily specify a loader extension for json or css (e.g. `json!a.json); currnetlly every reousere 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 notaion
Open issue: is this reported on declaration or use sites
Readonly properties
Suggestion
PR
Design
A property is allowed to have one of three states:
- readonly
- unknown
- mutable
readonly properties are declared using readonly
modifier, or as getters with no setters.
mutable properties are
readonly properties can not be assigned to if accessed directly. readonly properties are assignable to properties deemed "unknown", but not to mutable properties.
The rational here is ppl today do not have a way of expressing readonly, so existing typings do not really mean "mutable" by default. enforcing a default of mutability on these definitions will make using them with exiting typings much harder.
we still want to catch the most blatant violations of readonly, i.e. writing to a readonly property.
So:
interface I {
x: number;
}
class C {
get x() { return 1; }
}
var c = new C();
var i: I;
c.x = 1; // error, assigning to a readonly property
i = c; // No error, c is assignable to i
Next steps
-
Add the mutable modifier
-
Look into a
readonly
type operator to generate a readonly version of a type e.g.var x: readonly { a: string[] } x.a = []; // error; x.a.push(..); // error x["b"] = 0; // error