-
Notifications
You must be signed in to change notification settings - Fork 116
Create typings.d.ts #76
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
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.
this will be nice to have included with the npm module, thanks!
typings.d.ts
Outdated
@@ -0,0 +1,9 @@ | |||
declare module 'react-media' { | |||
interface MediaProps { | |||
query: 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.
query
should be a string or an object with string keys and values of number, string or boolean. looks like react-media will pass a string query to window.matchMedia(query)
, and will pass an object query directly to json2mq:
https://github.com/akiran/json2mq
maybe this:
export interface MediaQueryObject {
[id: string]: boolean | number | string;
}
export interface MediaProps {
query: string | MediaQueryObject;
...
}
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.
looks like query
can also accept an array of objects, so maybe this is better:
export interface MediaQueryObject {
[id: string]: boolean | number | string;
}
export interface MediaProps {
query: string | MediaQueryObject | MediaQueryObject[];
...
}
typings.d.ts
Outdated
interface MediaProps { | ||
query: string; | ||
defaultMatches?: boolean; | ||
children: ((matches: boolean) => JSX.Element) | JSX.Element |
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.
children
should be optional, and i believe these should specify React.ReactNode instead of JSX.Element
typings.d.ts
Outdated
query: string; | ||
defaultMatches?: boolean; | ||
children: ((matches: boolean) => JSX.Element) | JSX.Element | ||
} |
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.
we're also missing the optional render
prop:
render?: () => React.ReactNode;
typings.d.ts
Outdated
@@ -0,0 +1,9 @@ | |||
declare module 'react-media' { | |||
interface MediaProps { |
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.
don't forget to export the props interface so other components can access it
@swese44 Thanks for all the hints! I think I've fixed all of the issues. |
If you'd like to include this in the npm package you'll need to add it to the |
I've renamed the TS file to One final question: the typings now depend on |
index.d.ts
Outdated
@@ -0,0 +1,14 @@ | |||
declare module 'react-media' { |
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.
This line is unnecessary when you bundle typings with the package like we're doing here. The module name will be the one in package.json
.
package.json
Outdated
@@ -8,7 +8,8 @@ | |||
"files": [ | |||
"cjs", | |||
"esm", | |||
"umd" | |||
"umd", | |||
"index.d.ts" |
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.
You seem to be using tabs, but the rest of the file doesn't.
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.
Oops, my bad
@edorivai left two comments. Regarding dependencies, you should import them in your import { Component, ReactNode } from 'react'; |
@unindented Fixed both issues, and converted index.d.ts to spaces as well 🤓 |
The line |
Oh really, didn't know that. Nice! |
Btw, once everything looks okay, I'll happily squash everything into 1 commit, if you guys prefer that. |
Hi, Is this going to be merged? Is there a workaround to use the library in Typescript? Thanks |
@farzadmf workaround would be to copy/paste this file into your project: |
Thank you @edorivai , I thought of that 👍 ; just wanted to check if there's a "more official" way to do it :) |
@mjackson any chance of getting this merged, and a new release published? |
This is the TypeScript typing that we're using internally. Please let me know if changes are required.