Skip to content

Commit 9b5b46d

Browse files
committed
Add more precise types
1 parent f1767a3 commit 9b5b46d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/slots.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ interface SlotsToTypes {
150150

151151
type SlotKey = keyof SlotsToTypes;
152152

153-
const globalSlots = new WeakMap();
153+
const globalSlots = new WeakMap<Slots[keyof Slots]['usedBy'], Record<keyof Slots, Slots[keyof Slots]['value']>>();
154154

155-
function _GetSlots(container: Slots[SlotKey]['usedBy']) {
155+
function _GetSlots(container: Slots[keyof Slots]['usedBy']) {
156156
return globalSlots.get(container);
157157
}
158158

@@ -161,9 +161,9 @@ const GetSlotsSymbol = Symbol.for('@@Temporal__GetSlots');
161161
// expose GetSlots to avoid dual package hazards
162162
(globalThis as any)[GetSlotsSymbol] ||= _GetSlots;
163163

164-
const GetSlots = (globalThis as any)[GetSlotsSymbol];
164+
const GetSlots = (globalThis as any)[GetSlotsSymbol] as typeof _GetSlots;
165165

166-
function _CreateSlots(container: Slots[SlotKey]['usedBy']): void {
166+
function _CreateSlots(container: Slots[keyof Slots]['usedBy']): void {
167167
globalSlots.set(container, Object.create(null));
168168
}
169169

@@ -172,7 +172,7 @@ const CreateSlotsSymbol = Symbol.for('@@Temporal__CreateSlots');
172172
// expose CreateSlots to avoid dual package hazards
173173
(globalThis as any)[CreateSlotsSymbol] ||= _CreateSlots;
174174

175-
export const CreateSlots = (globalThis as any)[CreateSlotsSymbol];
175+
export const CreateSlots = (globalThis as any)[CreateSlotsSymbol] as typeof _CreateSlots;
176176

177177
// TODO: is there a better way than 9 overloads to make HasSlot into a type
178178
// guard that takes a variable number of parameters?
@@ -294,7 +294,7 @@ export function GetSlot<KeyT extends keyof Slots>(
294294
container: Slots[typeof id]['usedBy'],
295295
id: KeyT
296296
): Slots[KeyT]['value'] {
297-
const value = GetSlots(container)[id];
297+
const value = GetSlots(container)?.[id];
298298
if (value === undefined) throw new TypeError(`Missing internal slot ${id}`);
299299
return value;
300300
}
@@ -303,11 +303,15 @@ export function SetSlot<KeyT extends SlotKey>(
303303
id: KeyT,
304304
value: Slots[KeyT]['value']
305305
): void {
306-
const slot = GetSlots(container);
306+
const slots = GetSlots(container);
307307

308-
if (id in slot) {
308+
if (slots === undefined) {
309+
throw new TypeError(`Missing slots for the given container`);
310+
}
311+
312+
if (id in slots) {
309313
throw new TypeError(`${id} already has set`);
310314
}
311315

312-
slot[id] = value;
316+
slots[id] = value;
313317
}

0 commit comments

Comments
 (0)