Skip to content

Commit

Permalink
Add more precise types
Browse files Browse the repository at this point in the history
  • Loading branch information
tars0x9752 committed Oct 9, 2022
1 parent f1767a3 commit 829c58b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ interface SlotsToTypes {

type SlotKey = keyof SlotsToTypes;

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

function _GetSlots(container: Slots[SlotKey]['usedBy']) {
function _GetSlots(container: Slots[keyof Slots]['usedBy']) {
return globalSlots.get(container);
}

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

const GetSlots = (globalThis as any)[GetSlotsSymbol];
const GetSlots = (globalThis as any)[GetSlotsSymbol] as typeof _GetSlots;

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

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

export const CreateSlots = (globalThis as any)[CreateSlotsSymbol];
export const CreateSlots = (globalThis as any)[CreateSlotsSymbol] as typeof _CreateSlots;

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

if (id in slot) {
if (slots === undefined) {
throw new TypeError('Missing slots for the given container');
}

if (id in slots) {
throw new TypeError(`${id} already has set`);
}

slot[id] = value;
slots[id] = value;
}

0 comments on commit 829c58b

Please sign in to comment.