From 896e7e7bfdc44261e1df3defdbb9d1e15114da45 Mon Sep 17 00:00:00 2001 From: Simon Chan <1330321+yume-chan@users.noreply.github.com> Date: Wed, 11 May 2022 12:09:32 +0800 Subject: [PATCH] fix(struct): support custom TypeScript type for `uint8Array` --- libraries/struct/src/struct.ts | 66 ++++++++++--------- libraries/struct/src/types/buffer/base.ts | 6 +- .../struct/src/types/buffer/fixed-length.ts | 5 +- .../src/types/buffer/variable-length.ts | 8 ++- .../ts-package-builder/tsconfig.base.json | 4 +- 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/libraries/struct/src/struct.ts b/libraries/struct/src/struct.ts index 2de4704d..d6a07a3d 100644 --- a/libraries/struct/src/struct.ts +++ b/libraries/struct/src/struct.ts @@ -52,7 +52,7 @@ interface ArrayBufferLikeFieldCreator< * @param name Name of the field * @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String` * @param options Fixed-length array options - * @param typescriptType Type of the field in TypeScript. + * @param typeScriptType Type of the field in TypeScript. * For example, if this field is a string, you can declare it as a string enum or literal union. */ < @@ -63,7 +63,7 @@ interface ArrayBufferLikeFieldCreator< name: TName, type: TType, options: FixedLengthBufferLikeFieldOptions, - typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ): AddFieldDescriptor< TFields, TOmitInitKey, @@ -88,7 +88,7 @@ interface ArrayBufferLikeFieldCreator< name: TName, type: TType, options: TOptions, - typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ): AddFieldDescriptor< TFields, TOmitInitKey, @@ -118,7 +118,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator< >( name: TName, options: FixedLengthBufferLikeFieldOptions, - typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ): AddFieldDescriptor< TFields, TOmitInitKey, @@ -127,7 +127,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator< TName, FixedLengthBufferLikeFieldDefinition< TType, - FixedLengthBufferLikeFieldOptions + FixedLengthBufferLikeFieldOptions, + TTypeScriptType > >; @@ -139,7 +140,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator< >( name: TName, options: TOptions, - typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ): AddFieldDescriptor< TFields, TOmitInitKey, @@ -148,7 +149,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator< TName, VariableLengthBufferLikeFieldDefinition< TType, - TOptions + TOptions, + TTypeScriptType > >; } @@ -251,11 +253,11 @@ export class Struct< >( name: TName, type: TType, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.field( name, - new NumberFieldDefinition(type, _typescriptType), + new NumberFieldDefinition(type, typeScriptType), ); } @@ -267,12 +269,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Int8, - _typescriptType + typeScriptType ); } @@ -284,12 +286,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Uint8, - _typescriptType + typeScriptType ); } @@ -301,12 +303,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Int16, - _typescriptType + typeScriptType ); } @@ -318,12 +320,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Uint16, - _typescriptType + typeScriptType ); } @@ -335,12 +337,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Int32, - _typescriptType + typeScriptType ); } @@ -352,12 +354,12 @@ export class Struct< TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType'] >( name: TName, - typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.number( name, NumberFieldType.Uint32, - typescriptType + typeScriptType ); } @@ -368,11 +370,11 @@ export class Struct< >( name: TName, type: TType, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.field( name, - new BigIntFieldDefinition(type, _typescriptType), + new BigIntFieldDefinition(type, typeScriptType), ); } @@ -386,12 +388,12 @@ export class Struct< TTypeScriptType = BigIntFieldType['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.bigint( name, BigIntFieldType.Int64, - _typescriptType + typeScriptType ); } @@ -405,12 +407,12 @@ export class Struct< TTypeScriptType = BigIntFieldType['TTypeScriptType'] >( name: TName, - _typescriptType?: TTypeScriptType, + typeScriptType?: TTypeScriptType, ) { return this.bigint( name, BigIntFieldType.Uint64, - _typescriptType + typeScriptType ); } @@ -445,9 +447,10 @@ export class Struct< Uint8ArrayBufferFieldSubType > = ( name: PropertyKey, - options: any + options: any, + typeScriptType: any, ): any => { - return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options); + return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType); }; public string: BoundArrayBufferLikeFieldDefinitionCreator< @@ -458,9 +461,10 @@ export class Struct< StringBufferFieldSubType > = ( name: PropertyKey, - options: any + options: any, + typeScriptType: any, ): any => { - return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options); + return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType); }; /** diff --git a/libraries/struct/src/types/buffer/base.ts b/libraries/struct/src/types/buffer/base.ts index 9d6fe82a..871b8fa5 100644 --- a/libraries/struct/src/types/buffer/base.ts +++ b/libraries/struct/src/types/buffer/base.ts @@ -38,7 +38,8 @@ export abstract class BufferFieldSubType { +export class Uint8ArrayBufferFieldSubType + extends BufferFieldSubType { public static readonly Instance = new Uint8ArrayBufferFieldSubType(); protected constructor() { @@ -85,9 +86,10 @@ export abstract class BufferLikeFieldDefinition< TType extends BufferFieldSubType = BufferFieldSubType, TOptions = void, TOmitInitKey extends PropertyKey = never, + TTypeScriptType = TType["TTypeScriptType"], > extends StructFieldDefinition< TOptions, - TType['TTypeScriptType'], + TTypeScriptType, TOmitInitKey >{ public readonly type: TType; diff --git a/libraries/struct/src/types/buffer/fixed-length.ts b/libraries/struct/src/types/buffer/fixed-length.ts index ea050fa4..e00802d7 100644 --- a/libraries/struct/src/types/buffer/fixed-length.ts +++ b/libraries/struct/src/types/buffer/fixed-length.ts @@ -7,9 +7,12 @@ export interface FixedLengthBufferLikeFieldOptions { export class FixedLengthBufferLikeFieldDefinition< TType extends BufferFieldSubType = BufferFieldSubType, TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions, + TTypeScriptType = TType["TTypeScriptType"], > extends BufferLikeFieldDefinition< TType, - TOptions + TOptions, + never, + TTypeScriptType > { public getSize(): number { return this.options.length; diff --git a/libraries/struct/src/types/buffer/variable-length.ts b/libraries/struct/src/types/buffer/variable-length.ts index fae12aee..d9f168f2 100644 --- a/libraries/struct/src/types/buffer/variable-length.ts +++ b/libraries/struct/src/types/buffer/variable-length.ts @@ -26,11 +26,13 @@ export interface VariableLengthBufferLikeFieldOptions< export class VariableLengthBufferLikeFieldDefinition< TType extends BufferFieldSubType = BufferFieldSubType, - TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions + TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions, + TTypeScriptType = TType["TTypeScriptType"], > extends BufferLikeFieldDefinition< TType, TOptions, - TOptions['lengthField'] + TOptions['lengthField'], + TTypeScriptType > { public getSize(): number { return 0; @@ -47,7 +49,7 @@ export class VariableLengthBufferLikeFieldDefinition< public override create( options: Readonly, struct: StructValue, - value: TType['TTypeScriptType'], + value: TTypeScriptType, array?: Uint8Array ): VariableLengthBufferLikeStructFieldValue { return new VariableLengthBufferLikeStructFieldValue( diff --git a/toolchain/ts-package-builder/tsconfig.base.json b/toolchain/ts-package-builder/tsconfig.base.json index 92aff71d..7c52c7c7 100644 --- a/toolchain/ts-package-builder/tsconfig.base.json +++ b/toolchain/ts-package-builder/tsconfig.base.json @@ -3,7 +3,7 @@ "composite": true, /* Basic Options */ "target": "ES2022", // /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ - "module": "Node12", // /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + "module": "NodeNext", // /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ // /* Specify library files to be included in the compilation. */ "ESNext" ], @@ -34,7 +34,7 @@ "noUncheckedIndexedAccess": true, "resolveJsonModule": true, /* Module Resolution Options */ - "moduleResolution": "Node12", // /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + "moduleResolution": "NodeNext", // /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */