@@ -1099,11 +1099,11 @@ export class Sequence<T> extends Layout<T[]> {
1099
1099
*
1100
1100
* @augments {Layout }
1101
1101
*/
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 ] > [ ] ;
1104
1104
decodePrefixes : boolean ;
1105
1105
1106
- constructor ( fields : Layout < LayoutObject > [ ] , property ?: string , decodePrefixes ?: boolean ) {
1106
+ constructor ( fields : Layout < T [ keyof T ] > [ ] , property ?: string , decodePrefixes ?: boolean ) {
1107
1107
if ( ! ( Array . isArray ( fields )
1108
1108
&& fields . reduce ( ( acc , v ) => acc && ( v instanceof Layout ) , true ) ) ) {
1109
1109
throw new TypeError ( 'fields must be array of Layout instances' ) ;
@@ -1173,12 +1173,12 @@ export class Structure extends Layout<LayoutObject> {
1173
1173
}
1174
1174
1175
1175
/** @override */
1176
- decode ( b : Uint8Array , offset = 0 ) : LayoutObject {
1176
+ decode ( b : Uint8Array , offset = 0 ) : T {
1177
1177
checkUint8Array ( b ) ;
1178
- const dest = this . makeDestinationObject ( ) ;
1178
+ const dest = this . makeDestinationObject ( ) as T ;
1179
1179
for ( const fd of this . fields ) {
1180
1180
if ( undefined !== fd . property ) {
1181
- dest [ fd . property ] = fd . decode ( b , offset ) ;
1181
+ dest [ fd . property as keyof T ] = fd . decode ( b , offset ) ;
1182
1182
}
1183
1183
offset += fd . getSpan ( b , offset ) ;
1184
1184
if ( this . decodePrefixes
@@ -1194,15 +1194,15 @@ export class Structure extends Layout<LayoutObject> {
1194
1194
* If `src` is missing a property for a member with a defined {@link
1195
1195
* Layout#property|property} the corresponding region of the buffer is
1196
1196
* left unmodified. */
1197
- encode ( src : LayoutObject , b : Uint8Array , offset = 0 ) : number {
1197
+ encode ( src : T , b : Uint8Array , offset = 0 ) : number {
1198
1198
const firstOffset = offset ;
1199
1199
let lastOffset = 0 ;
1200
1200
let lastWrote = 0 ;
1201
1201
for ( const fd of this . fields ) {
1202
1202
let span = fd . span ;
1203
1203
lastWrote = ( 0 < span ) ? span : 0 ;
1204
1204
if ( undefined !== fd . property ) {
1205
- const fv = src [ fd . property ] ;
1205
+ const fv = src [ fd . property as keyof T ] ;
1206
1206
if ( undefined !== fv ) {
1207
1207
lastWrote = fd . encode ( fv , b , offset ) ;
1208
1208
if ( 0 > span ) {
@@ -2607,8 +2607,8 @@ export const f64be = ((property?: string) => new DoubleBE(property));
2607
2607
2608
2608
/** Factory for {@link Structure} values. */
2609
2609
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 ) ) ;
2612
2612
2613
2613
/** Factory for {@link BitStructure} values. */
2614
2614
export const bits
@@ -2617,7 +2617,7 @@ export const bits
2617
2617
/** Factory for {@link Sequence} values. */
2618
2618
export const seq
2619
2619
= ( < T > ( elementLayout : Layout < T > , count : number | ExternalLayout , property ?: string ) =>
2620
- new Sequence ( elementLayout , count , property ) ) ;
2620
+ new Sequence < T > ( elementLayout , count , property ) ) ;
2621
2621
2622
2622
/** Factory for {@link Union} values. */
2623
2623
export const union
0 commit comments