Skip to content

Commit 821453f

Browse files
authored
Improve typescript story (#541)
1 parent 41cbfb3 commit 821453f

File tree

10 files changed

+57
-50
lines changed

10 files changed

+57
-50
lines changed

ember-power-calendar/eslint.config.mjs

-5
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ export default ts.config(
7979
},
8080
extends: [...ts.configs.recommendedTypeChecked, ember.configs.gts],
8181
rules: {
82-
'@typescript-eslint/no-explicit-any': 'off',
83-
'@typescript-eslint/no-misused-promises': 'off',
84-
'@typescript-eslint/unbound-method': 'off',
85-
'@typescript-eslint/no-unsafe-member-access': 'off',
86-
'@typescript-eslint/await-thenable': 'off',
8782
'ember/no-runloop': 0,
8883
},
8984
},

ember-power-calendar/src/-private/utils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
PowerCalendarActions,
66
TPowerCalendarOnSelect,
77
} from '../components/power-calendar';
8-
import { add } from '../utils.ts';
8+
import { add, type NormalizeCalendarValue } from '../utils.ts';
99
import type { TPowerCalendarRangeOnSelect } from '../components/power-calendar-range';
1010
import type { TPowerCalendarMultipleOnSelect } from '../components/power-calendar-multiple.ts';
1111

@@ -17,7 +17,11 @@ export function publicActionsObject(
1717
| undefined,
1818
select: (day: CalendarDay, calendar: CalendarAPI, e: MouseEvent) => void,
1919
onCenterChange:
20-
| ((newCenter: any, calendar: CalendarAPI, event: MouseEvent) => void)
20+
| ((
21+
newCenter: NormalizeCalendarValue,
22+
calendar: CalendarAPI,
23+
event: MouseEvent,
24+
) => Promise<void>)
2125
| undefined,
2226
changeCenterTask: TaskForAsyncTaskFunction<
2327
unknown,
@@ -38,9 +42,9 @@ export function publicActionsObject(
3842
return changeCenterTask.perform(newCenter, calendar, e);
3943
};
4044
actions.changeCenter = changeCenter;
41-
actions.moveCenter = (step, unit, calendar, e) => {
45+
actions.moveCenter = async (step, unit, calendar, e) => {
4246
const newCenter = add(currentCenter, step, unit);
43-
return changeCenter(newCenter, calendar, e);
47+
return await changeCenter(newCenter, calendar, e);
4448
};
4549
}
4650

ember-power-calendar/src/components/power-calendar-multiple.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type {
2323
PowerCalendarAPI,
2424
PowerCalendarActions,
2525
PowerCalendarArgs,
26-
PowerCalendarSignature,
2726
SelectedDays,
2827
TCalendarType,
2928
} from './power-calendar.ts';
@@ -32,8 +31,9 @@ import type { ComponentLike } from '@glint/template';
3231
import type PowerCalendarService from '../services/power-calendar.ts';
3332

3433
export interface PowerCalendarMultipleAPI
35-
extends Omit<PowerCalendarAPI, 'selected'> {
34+
extends Omit<PowerCalendarAPI, 'selected' | 'DaysComponent'> {
3635
selected?: Date[];
36+
DaysComponent: ComponentLike<PowerCalendarMultipleDaysComponent>;
3737
}
3838

3939
export type TPowerCalendarMultipleOnSelect = (
@@ -43,18 +43,19 @@ export type TPowerCalendarMultipleOnSelect = (
4343
) => void;
4444

4545
interface PowerCalendarMultipleArgs
46-
extends Omit<PowerCalendarArgs, 'selected' | 'onSelect'> {
46+
extends Omit<PowerCalendarArgs, 'selected' | 'daysComponent' | 'onSelect'> {
4747
selected?: Date[];
48+
daysComponent?: string | ComponentLike<PowerCalendarMultipleDaysComponent>;
4849
onSelect?: TPowerCalendarMultipleOnSelect;
4950
}
5051

5152
interface PowerCalendarMultipleDefaultBlock extends PowerCalendarMultipleAPI {
52-
NavComponent: ComponentLike<any>;
53-
DaysComponent: ComponentLike<any>;
53+
NavComponent: ComponentLike<PowerCalendarNavComponent>;
54+
DaysComponent: ComponentLike<PowerCalendarMultipleDaysComponent>;
5455
}
5556

56-
interface PowerCalendarMultipleSignature
57-
extends Omit<PowerCalendarSignature, 'Args'> {
57+
interface PowerCalendarMultipleSignature {
58+
Element: HTMLElement;
5859
Args: PowerCalendarMultipleArgs;
5960
Blocks: {
6061
default: [PowerCalendarMultipleDefaultBlock];
@@ -68,8 +69,8 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
6869
@tracked _calendarType: TCalendarType = 'multiple';
6970
@tracked _selected?: SelectedDays;
7071

71-
navComponent: ComponentLike<any> = PowerCalendarNavComponent;
72-
daysComponent: ComponentLike<any> = PowerCalendarMultipleDaysComponent;
72+
navComponent = PowerCalendarNavComponent;
73+
daysComponent = PowerCalendarMultipleDaysComponent;
7374

7475
// Lifecycle hooks
7576
constructor(owner: Owner, args: PowerCalendarMultipleArgs) {
@@ -88,7 +89,7 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
8889
get publicActions(): PowerCalendarActions {
8990
return publicActionsObject(
9091
this.args.onSelect,
91-
this.select,
92+
this.select.bind(this),
9293
this.args.onCenterChange,
9394
this.changeCenterTask,
9495
this.currentCenter,
@@ -213,14 +214,17 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
213214
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
214215
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
215216
// @ts-expect-error Property '__powerCalendars'
217+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
216218
window.__powerCalendars[this.publicAPI.uniqueId] = this;
217219
}
218220
}
219221

220222
unregisterCalendar() {
221223
// @ts-expect-error Property '__powerCalendars'
224+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
222225
if (window && window.__powerCalendars?.[guidFor(this)]) {
223226
// @ts-expect-error Property '__powerCalendars'
227+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
224228
delete window.__powerCalendars[guidFor(this)];
225229
}
226230
}

ember-power-calendar/src/components/power-calendar-multiple/days.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ export default class PowerCalendarMultipleDaysComponent extends Component<PowerC
140140
scheduleOnce(
141141
'actions',
142142
this,
143-
this._updateFocused,
143+
this._updateFocused.bind(this),
144144
(e.target as HTMLElement).dataset['date'],
145145
);
146146
}
147147

148148
@action
149149
handleDayBlur(): void {
150-
scheduleOnce('actions', this, this._updateFocused, null);
150+
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
151151
}
152152

153153
@action

ember-power-calendar/src/components/power-calendar-range.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from '../utils.ts';
2323
import type {
2424
PowerCalendarAPI,
25-
PowerCalendarSignature,
2625
PowerCalendarArgs,
2726
TCalendarType,
2827
SelectedDays,
@@ -56,12 +55,12 @@ interface PowerCalendarRangeArgs
5655
}
5756

5857
export interface PowerCalendarRangeDefaultBlock extends PowerCalendarRangeAPI {
59-
NavComponent: ComponentLike<any>;
60-
DaysComponent: ComponentLike<any>;
58+
NavComponent: ComponentLike<PowerCalendarNavComponent>;
59+
DaysComponent: ComponentLike<PowerCalendarRangeDaysComponent>;
6160
}
6261

63-
interface PowerCalendarRangeSignature
64-
extends Omit<PowerCalendarSignature, 'Args' | 'Blocks'> {
62+
interface PowerCalendarRangeSignature {
63+
Element: HTMLElement;
6564
Args: PowerCalendarRangeArgs;
6665
Blocks: {
6766
default: [PowerCalendarRangeDefaultBlock];
@@ -82,8 +81,8 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
8281
@tracked _calendarType: TCalendarType = 'range';
8382
@tracked _selected?: SelectedDays;
8483

85-
navComponent: ComponentLike<any> = PowerCalendarNavComponent;
86-
daysComponent: ComponentLike<any> = PowerCalendarRangeDaysComponent;
84+
navComponent = PowerCalendarNavComponent;
85+
daysComponent = PowerCalendarRangeDaysComponent;
8786

8887
// Lifecycle hooks
8988
constructor(owner: Owner, args: PowerCalendarRangeArgs) {
@@ -102,7 +101,7 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
102101
get publicActions(): PowerCalendarActions {
103102
return publicActionsObject(
104103
this.args.onSelect,
105-
this.select,
104+
this.select.bind(this),
106105
this.args.onCenterChange,
107106
this.changeCenterTask,
108107
this.currentCenter,
@@ -306,19 +305,22 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
306305
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
307306
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
308307
// @ts-expect-error Property '__powerCalendars'
308+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
309309
window.__powerCalendars[this.publicAPI.uniqueId] = this;
310310
}
311311
}
312312

313313
unregisterCalendar() {
314314
// @ts-expect-error Property '__powerCalendars'
315+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
315316
if (window && window.__powerCalendars?.[guidFor(this)]) {
316317
// @ts-expect-error Property '__powerCalendars'
318+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
317319
delete window.__powerCalendars[guidFor(this)];
318320
}
319321
}
320322
}
321323

322-
function ownProp<T = { [key: string | number]: any }>(obj: T, prop: keyof T) {
324+
function ownProp<T = { [key: string | number]: never }>(obj: T, prop: keyof T) {
323325
return Object.prototype.hasOwnProperty.call(obj, prop);
324326
}

ember-power-calendar/src/components/power-calendar-range/days.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ export default class PowerCalendarRangeDaysComponent extends Component<PowerCale
126126
scheduleOnce(
127127
'actions',
128128
this,
129-
this._updateFocused,
129+
this._updateFocused.bind(this),
130130
(e.target as HTMLElement).dataset['date'],
131131
);
132132
}
133133

134134
@action
135135
handleDayBlur(): void {
136-
scheduleOnce('actions', this, this._updateFocused, null);
136+
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
137137
}
138138

139139
@action

ember-power-calendar/src/components/power-calendar.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { tracked } from '@glimmer/tracking';
33
import { action } from '@ember/object';
44
import { guidFor } from '@ember/object/internals';
55
import { inject as service } from '@ember/service';
6-
import { task } from 'ember-concurrency';
6+
import { task, type TaskInstance } from 'ember-concurrency';
77
import { assert } from '@ember/debug';
88
import type { ComponentLike } from '@glint/template';
99
import {
@@ -52,13 +52,13 @@ export interface PowerCalendarActions {
5252
newCenter: Date,
5353
calendar: CalendarAPI,
5454
event: MouseEvent,
55-
) => void;
55+
) => TaskInstance<void>;
5656
moveCenter?: (
5757
step: number,
5858
unit: TPowerCalendarMoveCenterUnit,
5959
calendar: PowerCalendarAPI,
6060
event: MouseEvent,
61-
) => void;
61+
) => Promise<void>;
6262
select?: (day: CalendarDay, calendar: CalendarAPI, event: MouseEvent) => void;
6363
}
6464

@@ -69,14 +69,14 @@ export type TPowerCalendarOnSelect = (
6969
) => void;
7070

7171
export interface PowerCalendarArgs {
72-
daysComponent?: string | ComponentLike<any>;
72+
daysComponent?: string | ComponentLike<PowerCalendarDaysComponent>;
7373
locale: string;
74-
navComponent?: string | ComponentLike<any>;
74+
navComponent?: string | ComponentLike<PowerCalendarNavComponent>;
7575
onCenterChange?: (
7676
newCenter: NormalizeCalendarValue,
7777
calendar: PowerCalendarAPI,
7878
event: MouseEvent,
79-
) => void;
79+
) => Promise<void>;
8080
onInit?: (calendar: PowerCalendarAPI) => void;
8181
onSelect?: TPowerCalendarOnSelect;
8282
selected?: SelectedDays;
@@ -85,8 +85,8 @@ export interface PowerCalendarArgs {
8585
}
8686

8787
export interface PowerCalendarDefaultBlock extends PowerCalendarAPI {
88-
NavComponent: ComponentLike<any>;
89-
DaysComponent: ComponentLike<any>;
88+
NavComponent: ComponentLike<PowerCalendarNavComponent>;
89+
DaysComponent: ComponentLike<PowerCalendarDaysComponent>;
9090
}
9191

9292
export type CalendarDay =
@@ -109,8 +109,8 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
109109
@tracked _calendarType: TCalendarType = 'single';
110110
@tracked _selected?: SelectedDays;
111111

112-
navComponent: ComponentLike<any> = PowerCalendarNavComponent;
113-
daysComponent: ComponentLike<any> = PowerCalendarDaysComponent;
112+
navComponent = PowerCalendarNavComponent;
113+
daysComponent = PowerCalendarDaysComponent;
114114

115115
// Lifecycle hooks
116116
constructor(owner: Owner, args: PowerCalendarArgs) {
@@ -129,7 +129,7 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
129129
get publicActions(): PowerCalendarActions {
130130
return publicActionsObject(
131131
this.args.onSelect,
132-
this.select,
132+
this.select.bind(this),
133133
this.args.onCenterChange,
134134
this.changeCenterTask,
135135
this.currentCenter,
@@ -206,14 +206,17 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
206206
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
207207
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
208208
// @ts-expect-error Property '__powerCalendars'
209+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
209210
window.__powerCalendars[this.publicAPI.uniqueId] = this;
210211
}
211212
}
212213

213214
unregisterCalendar() {
214215
// @ts-expect-error Property '__powerCalendars'
216+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
215217
if (window && window.__powerCalendars?.[guidFor(this)]) {
216218
// @ts-expect-error Property '__powerCalendars'
219+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
217220
delete window.__powerCalendars[guidFor(this)];
218221
}
219222
}

ember-power-calendar/src/components/power-calendar/days.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ export default class PowerCalendarDaysComponent extends Component<PowerCalendarD
137137
scheduleOnce(
138138
'actions',
139139
this,
140-
this._updateFocused,
140+
this._updateFocused.bind(this),
141141
(e.target as HTMLElement).dataset['date'],
142142
);
143143
}
144144

145145
@action
146146
handleDayBlur(): void {
147-
scheduleOnce('actions', this, this._updateFocused, null);
147+
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
148148
}
149149

150150
@action

ember-power-calendar/src/test-support/helpers.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { run } from '@ember/runloop';
21
import { assert } from '@ember/debug';
32
import { click, settled, find } from '@ember/test-helpers';
43
import { formatDate } from '../utils.ts';
@@ -49,7 +48,7 @@ function findComponentInstance(
4948
calendarGuid,
5049
);
5150
// @ts-expect-error Property '__powerCalendars'
52-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
51+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
5352
return window.__powerCalendars[calendarGuid];
5453
}
5554

@@ -68,9 +67,7 @@ export async function calendarCenter(
6867
!!onCenterChange,
6968
);
7069
const publicAPI = calendarComponent.publicAPI;
71-
run(() =>
72-
publicAPI.actions.changeCenter!(newCenter, publicAPI, {} as MouseEvent),
73-
);
70+
await publicAPI.actions.changeCenter!(newCenter, publicAPI, {} as MouseEvent);
7471
return settled();
7572
}
7673

ember-power-calendar/src/types/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import type EmberTruthRegistry from 'ember-truth-helpers/template-registry';
44
import type { EmbroiderUtilRegistry } from '@embroider/util';
55

66
export interface AssignRegistry {
7+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78
[key: string]: any;
89
}
910

1011
export interface ReadonlyRegistry {
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1113
[key: string]: any;
1214
}
1315

0 commit comments

Comments
 (0)