Skip to content

Commit 9f49117

Browse files
committed
fix: better type inference via getBox(es)
relates to #499
1 parent f412bd5 commit 9f49117

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

entries/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ export type SampleGroupEntryKind = InstanceOf<
8989
Extends<BoxRegistry['sampleGroupEntry'], typeof SampleGroupEntry>
9090
>;
9191

92+
export type AllRegisteredBoxes = {
93+
[K in AllIdentifiers]: K extends keyof BoxRegistry['box']
94+
? InstanceOf<BoxRegistry['box'][K]>
95+
: K extends keyof BoxRegistry['sampleEntry']
96+
? InstanceOf<BoxRegistry['sampleEntry'][K]>
97+
: K extends keyof BoxRegistry['sampleGroupEntry']
98+
? InstanceOf<BoxRegistry['sampleGroupEntry'][K]>
99+
: K extends keyof BoxRegistry['uuid']
100+
? InstanceOf<BoxRegistry['uuid'][K]>
101+
: never;
102+
};
103+
92104
export interface FragmentedTrack<TUser> {
93105
id: number;
94106
user: TUser;

src/isofile.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import { BoxRegistry } from '#/registry';
8484
import { MP4BoxStream } from '#/stream';
8585
import type {
8686
AllIdentifiers,
87+
AllRegisteredBoxes,
8788
BoxKind,
8889
Description,
8990
EntityGroup,
@@ -921,17 +922,17 @@ export class ISOFile<TSegmentUser = unknown, TSampleUser = unknown> {
921922
}
922923

923924
/* Find and return specific boxes using recursion and early return */
924-
getBox(type: AllIdentifiers) {
925+
getBox<T extends AllIdentifiers>(type: T): AllRegisteredBoxes[T] {
925926
const result = this.getBoxes(type, true);
926927
return result.length ? result[0] : undefined;
927928
}
928929

929-
getBoxes(type: AllIdentifiers, returnEarly: boolean) {
930-
const result: Array<Box> = [];
930+
getBoxes<T extends AllIdentifiers>(type: T, returnEarly: boolean) {
931+
const result: Array<AllRegisteredBoxes[T]> = [];
931932

932933
const sweep = (root: Box | ISOFile) => {
933934
if (root instanceof Box && root.type && root.type === type) {
934-
result.push(root);
935+
result.push(root as unknown as AllRegisteredBoxes[T]);
935936
}
936937

937938
const inner: Array<Box> = [];

0 commit comments

Comments
 (0)