Skip to content

Commit a4998d1

Browse files
author
Griko Nibras
committed
refactor: accept loose types on create function
Signed-off-by: Griko Nibras <[email protected]>
1 parent b33f594 commit a4998d1

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"devDependencies": {
3636
"@types/node": "^14",
3737
"@types/react": "^17",
38-
"@types/tailwindcss": "^2",
3938
"eslint": "^7",
4039
"eslint-config-kdnj": "^1",
4140
"husky": "^7",

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export type CreatorReturnType = {
8686
* export const { useBreakpoint, ... } = create({ sm: "640px", ... });
8787
* ```
8888
*/
89-
export function create<Screens extends { readonly [breakpoint: string]: string }>(screens: Screens) {
89+
export function create(screens: object | undefined) {
90+
if (!screens) {
91+
throw new Error("Failed to create breakpoint hooks, given `screens` value is invalid.");
92+
}
93+
9094
function useBreakpoint(breakpoint: string, defaultValue: boolean = false) {
9195
const [match, setMatch] = React.useState(() => defaultValue);
9296
const matchRef = React.useRef(defaultValue);
@@ -95,7 +99,8 @@ export function create<Screens extends { readonly [breakpoint: string]: string }
9599
if (!(isBrowser && "matchMedia" in window)) return undefined;
96100

97101
function track() {
98-
const value = (screens[breakpoint] as "") ?? "999999px";
102+
// @ts-expect-error accessing index with uncertain `screens` type
103+
const value = (screens[breakpoint] as string) ?? "999999px";
99104
const query = window.matchMedia(`(min-width: ${value})`);
100105
matchRef.current = query.matches;
101106
if (matchRef.current != match) {

0 commit comments

Comments
 (0)