Skip to content
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

Update typings for Typescript 2.0 #190

Merged
merged 7 commits into from
Dec 20, 2016
Merged

Conversation

AaronAsAChimp
Copy link
Contributor

@AaronAsAChimp AaronAsAChimp commented Nov 17, 2016

Description

Typescript 2.0 has a new mechanism for resolving types for modules. This PR updates the typings for react-datetime to be compatible with with the latest typescript. It also includes a couple of fixes and enhancements to the typings.

  • Restrict viewMode to the correct strings (or a number).
  • Improve typings for timeConstraints
  • Fix renderDay, renderMonth, renderYear to the correct return types.
  • Improve typings for inputProps, limiting it to only those allowed on input elements
  • Fix ReactDatetime to be a class

Motivation and Context

Fixes issue #181. Improves compatibility with this package.

Checklist

  • My change required changes to the documentation.
    • I have updated the documentation accordingly.
    • I have updated the TypeScript type definition accordingly. :P
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@@ -46,15 +67,11 @@ declare module ReactDatetime {
*/
locale?: string;
/*
Whether to interpret input times as UTC or the user's local timezone.
*/
utc?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this prop removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... a mistake.


//// <reference path="../moment/moment-node.d.ts" />
declare module "react-datetime" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are using double-quotes but on L17 you are using single-quotes. Does the module declaration require double-quotes? If not I think you should change them to stay consistent :)


/*
A stand-in type for Moment, this file currently has no way of guaranteeing
the existence of those typings.
Copy link
Collaborator

@simeg simeg Nov 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like comments like these 😌

@@ -0,0 +1,170 @@
import * as React from 'react';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the most appropriate place for this file? If I was looking for a test file in a repo I'd check in the tests folder (where our other tests are).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the recommendations from the DefinitelyTyped repository. This one doesn't necessarily apply here, I can move it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the DefinitelyTyped readme it says

File: foo-tests.ts
Purpose: This contains sample code which tests the typings. This code does not run, but it is type-checked.

And that's what's happening in your changes, right? I didn't get that at first but yeah if strictly the typings are being tested I can see them living in the folder they're in now.

I see the typings being a separate modular thing on top of the component, so if this is the case (please confirm) then I'd say just leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is definitely the case 😄

@@ -0,0 +1,170 @@
import * as React from 'react';
import { Moment } from 'moment';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is written with ES6 syntax. I prefer ES6 syntax, but none of the other files are written in this syntax. My first thought is that we should stay consistent, but at the same time this could be the first step to making everything into ES6 (which I plan on doing eventually). What are your thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using ES6 syntax, the code is cleaner and allows for better static analysis. It is also the preferred style for Typescript.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, let's go with ES6.

renderMonth={ (props, month, year, selectedDate) => {
return <td {...props}>{ month }</td>;
} }
renderYear={ ( props, year, selectedDate ) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have spaces in the arguments here that you don't have on L118 or L121 (sorry if this is a bit picky).

},
"files": [
"index.d.ts",
"react-datetime-tests.tsx"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If moving the test file this reference will have to be updated as well (just a friendly reminder).

@simeg
Copy link
Collaborator

simeg commented Nov 17, 2016

Hey @AaronAsAChimp, thanks for this awesome contribution! As you can see I added some comments to the code.

These are breaking changes for people using version < 2.0 right? Is there some way to make it compatible for both, or has the whole community already embraced version 2?

@AaronAsAChimp
Copy link
Contributor Author

You're correct they are breaking changes. DefinitelyTyped maintains a separate branch for types-2.0 to preserve the 1.8 typings. I wasn't able to find a way to make these typings work for both.

@simeg
Copy link
Collaborator

simeg commented Nov 18, 2016

I am trying to figure out if we can support both v1.8 and v2.0, but I can't really see a way where we don't have to maintain two branches/repos, something I'd prefer not having to do.

I am leaning towards merging this, because I like the tests and when looking at the breaking changes from 1.8 -> 2.0 there's not really a lot of them. @AaronAsAChimp what are your thoughts? Any idea on how we can support both versions?

@vutran
Copy link

vutran commented Nov 18, 2016

+1 for the TS 2.0 support. If people are still on TS 1.8, they most like are not ahead in updating their packages as well (in this case, react-datetime).

@simeg
Copy link
Collaborator

simeg commented Nov 18, 2016

@vutran that is true, but usually dependencies are marked with a carrot (^) which means it wont update to the next major version but minor and patch versions are ok. This PR would be a minor version bump for react-datetime but the TS version bump 1.8 => 2.0 is a major.

Maybe I'm overthinking this, I just don't want to answer to people asking why we broke their application. I welcome every opinon on this matter :)

@schickling
Copy link

schickling commented Nov 20, 2016

Would be great if @AaronAsAChimp could provide this via @types until this is merged. As in fact it's breaking our build since we're using Typescript 2.1.

@AaronAsAChimp
Copy link
Contributor Author

I would prefer we either:

  1. Commit to having the typings here for the latest typescript and move the 1.8 typings and any future legacy typings to DefinitelyTyped, or
  2. move all typings there and leave this repository to be JavaScript only.

Having them in both places would only lead to confusion, extra maintenance, and erroneous bug reports when someone inadvertently uses stale typings. Even if its only temporary. But Its not really up to me, its up to the maintainers and how much time they are willing to spend supporting these typings.

@simeg
Copy link
Collaborator

simeg commented Nov 22, 2016

@AaronAsAChimp I like the first option you are suggesting. In a few months we could look into only using v2.0+ in both DefinitelyTyped and here.

@AaronAsAChimp @vutran @schickling I am reading the instructions in the README for the DefinitelyTyped repo, and I'm getting the feeling that some of you people that actually use TS can publish the 1.8 version quicker and better. Does anyone feel up for it?

In what order do we do this? Can we merge this now and publish the 1.8 version to DefinitelyTyped later?

Thanks so much for the help and input everyone!

@@ -0,0 +1,171 @@
import * as React from 'react';
import { Moment } from 'moment';
import ReactDatetime = require('react-datetime');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be equal to
import * as ReactDatetime from 'react-datetime';

but it's not.. i get the following error:
[ts] Module ''react-datetime'' resolves to a non-module entity and cannot be imported using this construct.

I now get no errors in vscode using import ReactDatetime from 'react-datetime'; , but its not able to import it properly so it breaks during build

is it possible to make it work using the import * as name syntax?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronAsAChimp would you be able to answer this? Thanks so much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it took so long to reply, but I wanted to make sure I had it right. They are not equivalent.

The following is for CommonJS modules that use module.exports = Thing. This may be an ugly way of importing, but it is not within the scope of this PR to change it. It would also massively break the way non-typescript users require the module.

import ReactDatetime = require('react-datetime');

When you have multiple named exports module.exports = { Thing: Thing } or export class Thing { ... } it is imported like so.

import * as ReactDatetime from 'react-datetime';

Finally, when you have a default export: export default class Thing {...}

import ReactDatetime from 'react-datetime';

You can read more here
https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronAsAChimp That's what I suspected. Thanks for your input, and thanks a lot for creating the PR in DefinitelyTyped.

@bovan does this make sense?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronAsAChimp @simeg I suspect the documentation isn't up to date.. the require syntax is what typescript implemented before the ES6 syntax was ready.

The main issues here are:

  • Editors does not allow you to type import * as ReactDatetime from 'react-datetime'; which works
  • Editors allows you now to type: import ReactDatetime from 'react-datetime'; which doesnt work.

This indicates that there is something wrong with the typings. (Also all other imports we use have no problems with the import * as- syntax, and there's a lot).
I'm sure there is a way to make the typings work without rewriting the actual code itself, unfortunately I've not had time to do further research into this.

Anyway, the problem for us is that we use tslint with "no-var-requires": true, but i work around that with disable-line, so it's not game breaking for me, just a heads up..

@simeg
Copy link
Collaborator

simeg commented Dec 7, 2016

About this PR - I want to wait to see what happens to the PR in DefinitelyTyped. If it gets merged I will merge this but if not I will have to think about the next step.

@AaronAsAChimp
Copy link
Contributor Author

With the DefinitelyTyped pull request closed. Is this ready for merge?

I've updated the branch with the suggested changes.

@simeg
Copy link
Collaborator

simeg commented Dec 20, 2016

Sorry for the late response everyone. So we did end up with supporting both 1.8 and 2.0+ in the same repo. I am ok with that if that's the suggestion from the DefinitelyTyped people, so I'll go ahead and merge this PR.

Thanks a lot everyone who contributed and a big thank you to @AaronAsAChimp for everything.

@simeg simeg merged commit a20f583 into arqex:master Dec 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants