Skip to content

Commit c2effe1

Browse files
committed
Really map/list types, add empty overloads
1 parent 893dd29 commit c2effe1

5 files changed

Lines changed: 33 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.4.0
4+
5+
- **Breaking:** `SfallMap<K, V>` now requires `K extends string | number` and uses a mapped type `[key in K]: V` instead of open index signatures. This enforces type-safe key access — a `SfallMap<number, V>` no longer silently accepts string keys.
6+
- **Breaking:** All generic functions accepting `SfallMap` (`map_contains_key`, `array_keys`, `array_values`, `clone_array`, `array_append`, `array_concat`, `create_array_map`, `temp_array_map`, `create_lookup_map`, `temp_lookup_map`) now constrain `K extends string | number`.
7+
- Add no-arg overloads for `list<T>()` and `map<K, V>()` to create empty typed collections.
8+
39
## 0.3.0
410

511
- **Breaking:** Rename `folib/rp/*` to `folib/rpu/*` (Restoration Project, updated).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "folib",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "TypeScript bindings for Fallout 2",
55
"type": "module",
66
"files": [

src/sfall/lib.arrays.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { temp_array_list, temp_array_map, array_is_map } from "./sfall";
1313

1414
/** Check if key exists in map array */
15-
export function map_contains_key<K, V>(arrayMap: SfallMap<K, V>, key: K): boolean {
15+
export function map_contains_key<K extends string | number, V>(arrayMap: SfallMap<K, V>, key: K): boolean {
1616
for (let i = 0; i < len_array(arrayMap); i++) {
1717
if (array_key(arrayMap, i) == key) return true;
1818
}
@@ -51,7 +51,7 @@ export function array_pop<T>(array: SfallList<T>): T {
5151
}
5252

5353
/** Returns a temp list of keys from a given array */
54-
export function array_keys<K, V>(array: SfallMap<K, V>): SfallList<K>;
54+
export function array_keys<K extends string | number, V>(array: SfallMap<K, V>): SfallList<K>;
5555
export function array_keys<T>(array: SfallList<T>): SfallList<number>;
5656
export function array_keys(array: any): any {
5757
const len = len_array(array);
@@ -63,7 +63,7 @@ export function array_keys(array: any): any {
6363
}
6464

6565
/** Returns a temp list of values from a given array */
66-
export function array_values<K, V>(array: SfallMap<K, V>): SfallList<V>;
66+
export function array_values<K extends string | number, V>(array: SfallMap<K, V>): SfallList<V>;
6767
export function array_values<T>(array: SfallList<T>): SfallList<T>;
6868
export function array_values(array: any): any {
6969
const len = len_array(array);
@@ -118,7 +118,7 @@ export function copy_array<T>(src: SfallList<T>, srcPos: number, dest: SfallList
118118
}
119119

120120
/** Create a shallow copy of array as new temp array */
121-
export function clone_array<K, V>(array: SfallMap<K, V>): SfallMap<K, V>;
121+
export function clone_array<K extends string | number, V>(array: SfallMap<K, V>): SfallMap<K, V>;
122122
export function clone_array<T>(array: SfallList<T>): SfallList<T>;
123123
export function clone_array(array: any): any {
124124
const len = len_array(array);
@@ -251,7 +251,7 @@ export function array_fill<T>(arr: SfallList<T>, pos: number, count: number, val
251251

252252
/** Append all items from arr2 to arr1 */
253253
export function array_append<T>(arr1: SfallList<T>, arr2: SfallList<T>): SfallList<T>;
254-
export function array_append<K, V>(arr1: SfallMap<K, V>, arr2: SfallMap<K, V>): SfallMap<K, V>;
254+
export function array_append<K extends string | number, V>(arr1: SfallMap<K, V>, arr2: SfallMap<K, V>): SfallMap<K, V>;
255255
export function array_append(arr1: any, arr2: any): any {
256256
if (array_is_map(arr1)) {
257257
const len2 = len_array(arr2);
@@ -270,7 +270,7 @@ export function array_append(arr1: any, arr2: any): any {
270270

271271
/** Concat arrays into new temp array */
272272
export function array_concat<T>(arr1: SfallList<T>, arr2: SfallList<T>): SfallList<T>;
273-
export function array_concat<K, V>(arr1: SfallMap<K, V>, arr2: SfallMap<K, V>): SfallMap<K, V>;
273+
export function array_concat<K extends string | number, V>(arr1: SfallMap<K, V>, arr2: SfallMap<K, V>): SfallMap<K, V>;
274274
export function array_concat(arr1: any, arr2: any): any {
275275
return array_append(clone_array(arr1), arr2);
276276
}

src/sfall/sfall.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,22 +1079,22 @@ export function temp_array_list<T>(size: number): SfallList<T> {
10791079
}
10801080

10811081
/** Create a persistent map */
1082-
export function create_array_map<K, V>(): SfallMap<K, V> {
1082+
export function create_array_map<K extends string | number, V>(): SfallMap<K, V> {
10831083
return create_array(-1, 0) as unknown as SfallMap<K, V>;
10841084
}
10851085

10861086
/** Create a temporary map */
1087-
export function temp_array_map<K, V>(): SfallMap<K, V> {
1087+
export function temp_array_map<K extends string | number, V>(): SfallMap<K, V> {
10881088
return temp_array(-1, 0) as unknown as SfallMap<K, V>;
10891089
}
10901090

10911091
/** Create a persistent lookup map (see arrays.txt for details) */
1092-
export function create_lookup_map<K, V>(): SfallMap<K, V> {
1092+
export function create_lookup_map<K extends string | number, V>(): SfallMap<K, V> {
10931093
return create_array(-1, 2) as unknown as SfallMap<K, V>;
10941094
}
10951095

10961096
/** Create a temporary lookup map */
1097-
export function temp_lookup_map<K, V>(): SfallMap<K, V> {
1097+
export function temp_lookup_map<K extends string | number, V>(): SfallMap<K, V> {
10981098
return temp_array(-1, 2) as unknown as SfallMap<K, V>;
10991099
}
11001100

src/types.d.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,33 @@ export type HitResult = 0 | 1 | 2 | 3;
9898
/** Sfall list array - numeric index, iterable over values */
9999
export type SfallList<T> = {
100100
[index: number]: T;
101+
} & Iterable<T> & {
101102
readonly __brand: 'SfallList';
102-
} & Iterable<T>;
103+
};
103104

104105
/** Sfall map array - key/value pairs, iterable as [key, value] tuples */
105-
export type SfallMap<K, V> = {
106-
[key: string]: V;
107-
[key: number]: V;
106+
export type SfallMap<K extends string | number, V> = {
107+
[key in K]: V;
108+
} & Iterable<[K, V]> & {
108109
readonly __brand: 'SfallMap';
109-
} & Iterable<[K, V]>;
110+
};
110111

111112
/**
112113
* Create a typed list from items. Transpiles to array literal [a, b, c].
113114
*/
114115
export declare function list<T>(...items: T[]): SfallList<T>;
115116

117+
/**
118+
* Create an empty typed list. Transpiles to empty array literal [].
119+
*/
120+
export declare function list<T>(): SfallList<T>;
121+
116122
/**
117123
* Create a typed map from object literal. Transpiles to map literal {a: 1}.
118124
*/
119125
export declare function map<K extends string | number, V>(obj: Record<K, V>): SfallMap<K, V>;
126+
127+
/**
128+
* Create an empty typed map. Transpiles to empty map literal {}.
129+
*/
130+
export declare function map<K extends string | number, V>(): SfallMap<K, V>;

0 commit comments

Comments
 (0)