Comments (18)
Yes, sorry, you was right, I incorrectly imported the module. Finally it works for me.
But, honestly, I have doubts. Your variant works fine, but syntax import * as timeago from 'timeago.js'
is quite redundant. It's readed as "import all the members as timeago namespace\variable", but in fact there is only one exported member - timeago factory. I think, that interfaces TimeAgoConstructor
and TimeAgo
shouldn't be part of the timeago factory, but as separate entities. Also such typings add some trash (interfaces) to autocomplite in IDE.
Second moment - let's look on the problem from other side. If we write clear typescript file:
// hello.ts
export default function hello() {};
and compile it with such ts config:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
}
}
Generated js
file will be:
"use strict";
exports.__esModule = true;
var Hello = (function () {
function Hello() {
}
return Hello;
}());
exports["default"] = Hello;
As far as we see, default
becomes a member of exports, and this file becomes ES6-import style compatible. The same story if we try to compile such code with babel:
// hello.js
export default function hello() {};
// hello.transpiled.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = hello;
function hello() {};
Both Typescript and Babel (webpack 2 does the same, but output is quite bigger) transpile default ES6 style import to ES5 style by adding default
property to exports. So, if we want timeago to become compatible with ES6 default import (I mean support syntax like import timeago from 'timeago.js'
), we need modify a module pattern a little:
// now
if (typeof module === 'object' && module.exports)
module.exports = factory(root);
// will become
if (typeof module === 'object' && module.exports) {
module.exports = factory(root); // nodejs support
module.exports['default'] = module.exports; // es6 support
}
@hustcc It will also solve this issue #105
If we do so, then typings that are currently in the project will become working.
So, we should decide:
- Should we support ES6 default import compatible with commonjs?
1.1. If yes, then leave typings as they are for now and modify module pattern implementation. Issue #105 will be solved itself
1.2. If no, accept PR by @rileystroud that fixes typings, no other modification is needed. Issue #105 needs more research.
Probably I misunderstand something, correct me if I'm wrong
from timeago.js.
Can you do something to help me by a pr?
from timeago.js.
hope, #107 can help
from timeago.js.
666
from timeago.js.
@hustcc lol, what's that?
from timeago.js.
6 has the same pronunciation with 牛 in Chinese, 牛 is cow, but has meaning of perfect, or very good in somewhere.
666(three 6) means very very very good, and admire you for your develop ability.
from timeago.js.
@hustcc wow, very instructive, thank you
@rileystroud can you please try this branch and confirm if it works for you? You can install it through npm like:
npm install hustcc/timeago.js#ts-support
from timeago.js.
@rileystroud merge into master, so just npm install hustcc/timeago.js
for test.
from timeago.js.
@hustcc probably you need to update npm version for that and update dist files (sorry, I didn't make it in #110, because I didn't know you could merge it so quickly)
from timeago.js.
It's not working for me on my first pass. I'll get back to it in a couple hours
from timeago.js.
@rileystroud how did you install it? can you check if timeago.d.ts
present in your node_modules/timeago.js
folder?
from timeago.js.
It installed fine. The typescript code wasn't compiling to the correct javascript.
import timeago from 'timeago.js'
const timeagoInstance = timeago()
timeagoInstance.format('01-01-2017')
compiled to
const timeago_js_1 = require("timeago.js");
const timeagoInstance = timeago_js_1.default();
timeagoInstance.format('01-01-2017');
but there is no default function defined.
With #112,
import * as timeago from 'timeago.js'
const timeagoInstance = timeago()
timeagoInstance.format('01-01-2017')
compiles to the expected
const timeago = require("timeago.js");
const timeagoInstance = timeago();
timeagoInstance.format('01-01-2017');
from timeago.js.
I checked out your version (#112), it doesn't even compile with typescript ^2.2.1:
app.ts(1,8): error TS1192: Module '"timeago-test/node_modules/timeago.js/timeago"' has no default export.
Command failed: tsc
Which version of typescript do you use?
from timeago.js.
I'm on typescript 2.2.1
@likerRr Did you update the import statement as above?
from timeago.js.
@rileystroud @likerRr I think export default is the reason. So I think method 1.1 is better, also can solve #105.
from timeago.js.
Shouldn't the constructor return an instance of itself?
(Notice the this
in the new()
signatures.)
/// <reference types="jquery" />
type TDate = Date | string | number;
export interface TimeAgoConstructor {
new (): this;
new (nowDate: TDate, defaultLocale?: string): this;
format(date: TDate, locale?: string): string;
render<T>(nodes: Node | NodeList | JQuery, locale?: string): void;
setLocale(locale: string): void;
}
export interface TimeagoFactory {
(): TimeAgoConstructor;
(nowDate: TDate, defaultLocale?: string): TimeAgoConstructor;
cancel(node?: Node | JQuery): void;
register(locale: string, localeFunc: Function): void;
}
declare let timeagoFactory: TimeagoFactory;
export default timeagoFactory;
from timeago.js.
I hope this
keyword is implied. Anyway now it doesn't generate compilation errors
from timeago.js.
@estk constructor is removed since the timeagoFactory
returns an instance #126
from timeago.js.
Related Issues (20)
- .js.map source map parsing warnings in Webpack source-map-loader HOT 4
- Add possibility to change suffix / prefix like "ago" and "in" HOT 1
- CDN Error: Uncaught ReferenceError: exports is not defined HOT 3
- Disable for @media print?
- show less than 10 seconds integers instead of "right now"?
- Languages
- Time issue after appending row HOT 1
- JS Pure HOT 1
- is not being translated to pt_BR HOT 1
- timeago().render(element) not updating
- Cannot register lang with CommonJS module in express project HOT 2
- Documentation for importing languages HOT 1
- Kurdish locale
- cs translations is not in the full.min.js bundle
- Croatian, Serbian locale
- Is this project actively maintained? HOT 4
- locale 是否可以兼容支持小写? HOT 3
- WARNING in ./node_modules/timeago.js/esm/realtime.js
- Has timeago.org become a gambling site? HOT 2
- Error: ENOENT: no such file or directory, open 'E:\Projects\Code-Crafters\node_modules\timeago.js\src\format.ts' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timeago.js.