Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit ec0095e

Browse files
committed
add type to structs
1 parent ae9ef3b commit ec0095e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/Layout.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,11 @@ export class Sequence<T> extends Layout<T[]> {
10991099
*
11001100
* @augments {Layout}
11011101
*/
1102-
export class Structure extends Layout<LayoutObject> {
1103-
fields: Layout<LayoutObject>[];
1102+
export class Structure<T> extends Layout<T> {
1103+
fields: Layout<T[keyof T]>[];
11041104
decodePrefixes: boolean;
11051105

1106-
constructor(fields: Layout<LayoutObject>[], property?: string, decodePrefixes?: boolean) {
1106+
constructor(fields: Layout<T[keyof T]>[], property?: string, decodePrefixes?: boolean) {
11071107
if (!(Array.isArray(fields)
11081108
&& fields.reduce((acc, v) => acc && (v instanceof Layout), true))) {
11091109
throw new TypeError('fields must be array of Layout instances');
@@ -1173,12 +1173,12 @@ export class Structure extends Layout<LayoutObject> {
11731173
}
11741174

11751175
/** @override */
1176-
decode(b: Uint8Array, offset = 0): LayoutObject {
1176+
decode(b: Uint8Array, offset = 0): T {
11771177
checkUint8Array(b);
1178-
const dest = this.makeDestinationObject();
1178+
const dest = this.makeDestinationObject() as T;
11791179
for (const fd of this.fields) {
11801180
if (undefined !== fd.property) {
1181-
dest[fd.property] = fd.decode(b, offset);
1181+
dest[fd.property as keyof T] = fd.decode(b, offset);
11821182
}
11831183
offset += fd.getSpan(b, offset);
11841184
if (this.decodePrefixes
@@ -1194,15 +1194,15 @@ export class Structure extends Layout<LayoutObject> {
11941194
* If `src` is missing a property for a member with a defined {@link
11951195
* Layout#property|property} the corresponding region of the buffer is
11961196
* left unmodified. */
1197-
encode(src: LayoutObject, b: Uint8Array, offset = 0): number {
1197+
encode(src: T, b: Uint8Array, offset = 0): number {
11981198
const firstOffset = offset;
11991199
let lastOffset = 0;
12001200
let lastWrote = 0;
12011201
for (const fd of this.fields) {
12021202
let span = fd.span;
12031203
lastWrote = (0 < span) ? span : 0;
12041204
if (undefined !== fd.property) {
1205-
const fv = src[fd.property];
1205+
const fv = src[fd.property as keyof T];
12061206
if (undefined !== fv) {
12071207
lastWrote = fd.encode(fv, b, offset);
12081208
if (0 > span) {
@@ -2607,8 +2607,8 @@ export const f64be = ((property?: string) => new DoubleBE(property));
26072607

26082608
/** Factory for {@link Structure} values. */
26092609
export const struct
2610-
= ((fields: Layout<LayoutObject>[], property?: string, decodePrefixes?: boolean) =>
2611-
new Structure(fields, property, decodePrefixes));
2610+
= (<T>(fields: Layout<T[keyof T]>[], property?: string, decodePrefixes?: boolean) =>
2611+
new Structure<T>(fields, property, decodePrefixes));
26122612

26132613
/** Factory for {@link BitStructure} values. */
26142614
export const bits
@@ -2617,7 +2617,7 @@ export const bits
26172617
/** Factory for {@link Sequence} values. */
26182618
export const seq
26192619
= (<T>(elementLayout: Layout<T>, count: number | ExternalLayout, property?: string) =>
2620-
new Sequence(elementLayout, count, property));
2620+
new Sequence<T>(elementLayout, count, property));
26212621

26222622
/** Factory for {@link Union} values. */
26232623
export const union

0 commit comments

Comments
 (0)