Skip to content

Commit eded216

Browse files
committed
fix TS issues
1 parent 0da7318 commit eded216

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ const DEFAULT_ENV: Parameters<typeof matches>[1] = {
1010
dppx: 1,
1111
};
1212

13+
type PixelFeature = "width" | "height" | "deviceWidth" | "deviceHeight";
1314
const PIXEL_FEATURES = ["width", "height", "deviceWidth", "deviceHeight"];
15+
16+
const isPixelFeature = (key: string): key is PixelFeature => PIXEL_FEATURES.includes(key);
17+
1418
const convertStateToEnv = (state: MediaState): Parameters<typeof matches>[1] => {
1519
const env = { ...DEFAULT_ENV };
1620

1721
for (const [key, value] of Object.entries(state)) {
18-
if (PIXEL_FEATURES.includes(key)) {
19-
env[key + "Px"] = value;
22+
if (isPixelFeature(key)) {
23+
env[`${key}Px`] = value as number;
2024
} else {
21-
env[key] = value;
25+
(env[key as Exclude<Feature, PixelFeature>] as any) = value;
2226
}
2327
}
2428

@@ -109,7 +113,7 @@ export const matchMedia: typeof window.matchMedia = (query: string) => {
109113
},
110114
media: query,
111115
onchange: null,
112-
addEventListener: (event, callback, options) => {
116+
addEventListener: (event: string, callback: EventListener, options?: boolean | AddEventListenerOptions) => {
113117
if (event === "change" && callback) {
114118
const isAlreadyListed = callbacks.has(callback);
115119
callbacks.add(callback);
@@ -132,7 +136,7 @@ export const matchMedia: typeof window.matchMedia = (query: string) => {
132136
onces.add(callback);
133137
}
134138
},
135-
removeEventListener: (event, callback) => {
139+
removeEventListener: (event: string, callback: EventListener) => {
136140
if (event === "change") removeListener(callback);
137141
},
138142
dispatchEvent: (event: MediaQueryListEvent) => {
@@ -196,11 +200,11 @@ export class MediaQueryListEvent extends EventCompat {
196200
}
197201

198202
// Cannot use MediaState here as setMedia is exposed in the API
199-
export const setMedia = (media: MediaState) => {
203+
export const setMedia = (media: MediaState): void => {
200204
const changedFeatures = new Set<Feature>();
201205
Object.keys(media).forEach((feature) => {
202206
changedFeatures.add(feature as Feature);
203-
state[feature] = media[feature];
207+
(state[feature as Feature] as any) = media[feature as Feature];
204208
});
205209

206210
// If we are trying to change the `width` but not the `deviceWidth`

0 commit comments

Comments
 (0)