-
Notifications
You must be signed in to change notification settings - Fork 563
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
Add TypeScript declaration. #103
Conversation
Any further thoughts on this @JedWatson? |
It would be nice if there were definitions for the alternate How does this look for
|
@cwmacdon looks good to me — I'd love to see that get merged in too 👍. |
@bradleyayers is there any reason these definitions don't match the ones on DefinitelyTyped 100%? Also, the publishing doc states the following:
Perhaps you can add it? {
"types": "./index.d.ts"
} |
@jedmao I'm not sure I considered just copying over the @types/ code, maybe I wanted to avoid licensing issues, but using it (or matching it) indeed seems more desirable than having a set of deviating definitions. Having the .d.ts placed next to the index.js should indeed work. I'll give that a go and update the PR |
It will work, but they still recommend being explicit about it. I think what i was trying to say before its that if what you have here is better than the one on DT, perhaps you should submit a PR to DT first so that they are in sync. If you do so, submit to the types-2.0 branch. |
I'll compare and take the best and upstream if necessary. Thanks |
Any progress on this @bradleyayers @jedmao? Anything I can do? |
@cwmacdon I haven't had time to get back to this. But what you could do is to look into whether we can just copy the DefinitionTyped definitions into If we can, raise a PR to do so 😄 |
@bradleyayers So I tried copying the Definitely Typed version into my Then I wrote a file to use it
and it gave the error The definition that you have included above works perfectly. @adidahiya essentially wrote the DefinitelyTyped version, maybe he can weigh in? |
there are no licensing issues. I think you are free to copy the definitions I wrote for DefinitelyTyped, just make sure to add the package to |
index.d.ts
Outdated
@@ -0,0 +1,9 @@ | |||
type ClassValue = string | number | ClassDictionary | ClassArray; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make this match the DT typings. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a8dc31d5a2a55bf458834680d4653bc452949d84/classnames/index.d.ts
index.d.ts
Outdated
type ClassValue = string | number | ClassDictionary | ClassArray; | ||
|
||
interface ClassDictionary { | ||
[id: string]: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, this needs to allow undefined
and null
. also, personal preference (generally in the TS community), can you use 4-space indentation instead of tabs?
index.d.ts
Outdated
|
||
interface ClassArray extends Array<ClassValue> { } | ||
|
||
export default function(...classes: ClassValue[]): string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most consumers are currently doing import * as classNames from "classnames";
, and this will break that. actually I don't even think this is the shape of the export; see this part of the DT readme for guidance.
$ node -p 'require("classnames").default'
undefined
$ node -p 'require("classnames")'
[Function: classNames]
you should use the export =
syntax from my DT typings instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adidahiya There are many ways to do this...
import classNames from 'classNames';
import * as classNames from 'classNames';
import classNames = require('classNames');
Is there a "best" way? Is there a way to write the definition to work with all of the above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both import classNames from 'classNames';
and import * as classNames from 'classNames';
are generally OK. The last one that uses require()
is discouraged because it is old CommonJS-style syntax.
but, like I said, the DefinitelyTyped README stipulates that you should not declare a default export unless require("classnames").default
actually returns the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adidahiya Thanks for your input here. Good to know.
Looking at your definition on DefinitelyTyped, what is the purpose of the classnames
module wrapped around the export?
declare module "classnames" {
export = classNames
}
I think this is the underlying reason why @bradleyayers and myself strayed from using the definitions on DefinitelyTyped? If we remove this wrapping module, then we can just import * as classNames from 'classNames';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you don't need that part. That was a remnant from before we had @types
on NPM -- when you use those typings through tsd
or the typings
tool, you would need the ambient declaration of the "classnames"
module. Now that we have node-style resolution of index.d.ts, that part isn't needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adidahiya Would you recommend updating the DefinitelyTyped definition to remove it as well? Or should we just leave it out in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, updating the DT definition sounds like a good idea now that the repo has switched over to @types
completely. Once a release of classnames is made that includes definitions though, we should remove those typings from DT (and update notNeededPackages.json in the same PR).
@bradleyayers Thanks to input from @adidahiya and @jedmao, Could you update the PR with the changes outlined below?
Then once this is complete and merged, a PR to DefinitelyTyped will be needed to add classNames to the notNeededPackages.json to help users know that the types are included in this package now. |
@JedWatson Any chance a new release could be cut with this PR? |
@dcousens Perhaps you could merge this PR? I see you've kind of taken over merging PRs since mid last year. |
I'm waiting on @JedWatson to add me as a contributor for publishing to |
bump again here @JedWatson @dcousens, thanks |
It would be great if it was merged. What are we waiting for? |
This PR just merged in DT so you should be able to pick up the latest |
@adidahiya Thanks for the heads up. I have doubts this will ever get merged in here, so @types/classnames will be the best we can get. |
@bradleyayers can you tell me why you decided to go with export = classNames; instead of export default, I'm curious this does not work with allowSyntheticDefaultImports = false witch is default setting in ts. webpack can handle this when I do import * as classNames from 'classnames' but rollup will say
with allowSyntheticDefaultImports = true both works fine using this import classNames from 'classnames' export = has many issues and it requires this kind of import import classNames = require('classnames') (https://www.typescriptlang.org/docs/handbook/modules.html) OMG import default does not work with jest and wallaby :) I'm going to kill myself :) |
@maciejw you probably want the new
Note that this only affects type checking, not JS emit. See the DefinitelyTyped README for info about why |
@adidahiya indeed I have ts 2.7, but I did not change this flag... this esModuleInterop flag break my postcss modules in jest. witch worries me gravely:) now, what I did is I copied d.ts locally and declared it as another module and that imported types only import * as ns from 'classnames-ts';
const classNames : ns.ClassNamesFn = require('classnames');
export { classNames }; this works fine only with require in all those environments: jest, rollup, webpack |
@maciejw years ago when I opened this PR I used However since then the library has added support for ES modules by way of a default export (915186a). Unfortunately |
@bradleyayers I stumbled upon similar case with not callable export = in invariant zertosh/invariant#32 library... I'm sure not last one ;) to sum it up: |
Isn't it about time this library drop support for dead/unmaintained Node.js versions? It seems only Node.js v0.10 and 0.12 are failing this build. |
@maciejw I've responded in zertosh/invariant#32 (comment) |
Can we not merge this to |
Hi thanks for the questions. In regards to whether it can be merged to DefinitelyTyped someone will need to check the DefiniteyTyped repository to confirm it is not already there. I would be surprised if someone has not already contributed them there. In regards to why the types should be in this repository I suspect the argument for having the types in this repository may simply boil down to https://en.m.wikipedia.org/wiki/Cohesion_(computer_science). In regards to the maintainers not using TypeScript, I empathise with their plight and would not want them to take on any burden they do not feel comfortable with. They do amazing work for free and they should be not bullied into anything. In regards to championing the work I believe this PR may qualify. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@jedmao can you rebase your PR off this?
Any idea when this will be merged or is there anything I can do to help get this merged? I have not been able to get types working this library or by using Using: import classnames from 'classnames'; with |
@ssylvia you should try enabling both |
Wait, these were copied from DefinitelyTyped... Please add the copyright notice to the top of this file, it should be enough - though I am not a lawyer - as the LICENSE is the same. Otherwise I'll revert. |
Is there a reason
With the stricter types you have to do:
A bit of a nit-pick I guess, but the first one is a tad shorter and the library itself allows Also it looks like the latest published version to npm does not contain the TypeScript definitions. |
Still not released. |
Still :D |
@FDiskas it's released |
This resolves #102