Closed rekna1 closed 7 years ago
Not sure why you're running into these issues - consider filing a bug on the TypeScript repo, since that shouldn't happen.
For people who don't have "node_modules" excluded, and who prefer to continue using an in-repo moment.d.ts file, what worked for me to fix the import error is to replace the export = moment;
line in the d.ts file with export default moment;
.
This fixes the "module has no default export" error for me in the files which use the module (by means of import moment from "moment";
), and provides full intellisense.
In contrast, the declare module 'moment'{ export = moment; }
solution in the OP above provides the intellisense, but causes a "declaration or statement expected error" error for me inside the moment.d.ts file itself.
*==========
My TypeScript: 2.1.4
My .tsconfig:
{
"compilerOptions": {
"target": "es2015",
"allowJs": true,
"jsx": "react",
"noImplicitAny": false,
"moduleResolution": "node",
"watch": true,
//...
},
// ...
}
I found that if in my typescript base object I put
/// <reference path="../../node_modules/moment/moment.d.ts" />
I can reference the moment's d.ts however to get it to work I have to add
export as namespace moment;
at the top of the moment/moment.d.ts file and all compiles and works as expected.
Since at @DanielRosenwasser is on the TypeScript team, and he's saying this should be over there, I'm going to close this.
I think this issue should be reopen. And the line export as namespace moment;
should be added to moment.d.ts, as most other @types definitions do (source-map, angular, etc).
And it still is a problem if you include use moment
in an external lib.
I can't see this is a typescript problem. Other libraries (e.g. angular) provide a .d.ts file in the @types repository of npm and they seem to work just fine with tsconfig module="es6". Typescripts recommended way of providing typescript definition files is npm@types so why can't moment follow their recommendations ?
Instead of modifying the moment.d.ts directly, we found that defining a moment.shim.d.ts file in the src/ directory fixed the issue with the following code fixed the issue:
import * as moment from "moment";
export as namespace moment;
export = moment;
I'm still having problems with importing moment in a project with es6 modules in typescript.
It is common that node_modules is listed in the exclude compiler options, so moment.d.ts can not be found with this configuration.
I've found that I can get it working, when I copy moment.d.ts to @types/moment/moment.d.ts (@types is not ignored by typescript) and when I replace
export = moment;
withdeclare module 'moment'{ export = moment; }
My tsconfig.json is ` { "compileOnSave": true,
}` Maybe you could consider following the latest guidelines for typescript declaration files and publish the type declarations to the npm repository @types (or definitelytyped, as they are picked up by @types ) and changing the declare statement (which is in line with the way other libraries do it, e.g. jQuery)