Skip to content

Commit a621209

Browse files
authored
refactor: Explicit return types (#43)
1 parent 8070a22 commit a621209

File tree

14 files changed

+65
-46
lines changed

14 files changed

+65
-46
lines changed

packages/typed-binary/jsr.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@iwoplaza/typed-binary",
3+
"version": "4.3.0-alpha.0",
4+
"license": "MIT",
5+
"exports": "./src/index.ts",
6+
"publish": {
7+
"include": ["./README.md", "./package.json", "./src/**/*.ts"],
8+
"exclude": ["./src/test", "./**/*.test.ts"]
9+
}
10+
}

packages/typed-binary/src/io/bufferIOBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class BufferIOBase {
3636
this.dataView = new DataView(unwrapped.buffer);
3737
}
3838

39-
get currentByteOffset() {
39+
get currentByteOffset(): number {
4040
return this.byteOffset;
4141
}
4242

packages/typed-binary/src/io/bufferReader.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,59 @@ export class BufferReader extends BufferIOBase implements ISerialInput {
1313
return this._cachedTextDecoder;
1414
}
1515

16-
readBool() {
16+
readBool(): boolean {
1717
return this.dataView.getUint8(this.byteOffset++) !== 0;
1818
}
1919

20-
readByte() {
20+
readByte(): number {
2121
return this.dataView.getUint8(this.byteOffset++);
2222
}
2323

24-
readInt8() {
24+
readInt8(): number {
2525
return this.dataView.getInt8(this.byteOffset++);
2626
}
2727

28-
readUint8() {
28+
readUint8(): number {
2929
return this.dataView.getUint8(this.byteOffset++);
3030
}
3131

32-
readInt16() {
32+
readInt16(): number {
3333
const value = this.dataView.getInt16(this.byteOffset, this.littleEndian);
3434
this.byteOffset += 2;
3535
return value;
3636
}
3737

38-
readUint16() {
38+
readUint16(): number {
3939
const value = this.dataView.getUint16(this.byteOffset, this.littleEndian);
4040
this.byteOffset += 2;
4141
return value;
4242
}
4343

44-
readInt32() {
44+
readInt32(): number {
4545
const value = this.dataView.getInt32(this.byteOffset, this.littleEndian);
4646
this.byteOffset += 4;
4747
return value;
4848
}
4949

50-
readUint32() {
50+
readUint32(): number {
5151
const value = this.dataView.getUint32(this.byteOffset, this.littleEndian);
5252
this.byteOffset += 4;
5353
return value;
5454
}
5555

56-
readFloat16() {
56+
readFloat16(): number {
5757
const value = this.dataView.getUint16(this.byteOffset, this.littleEndian);
5858
this.byteOffset += 2;
5959
return float16ToNumber(value);
6060
}
6161

62-
readFloat32() {
62+
readFloat32(): number {
6363
const value = this.dataView.getFloat32(this.byteOffset, this.littleEndian);
6464
this.byteOffset += 4;
6565
return value;
6666
}
6767

68-
readString() {
68+
readString(): string {
6969
// Looking for the 'NULL' byte.
7070
let strLength = 0;
7171
while (this.byteOffset + strLength < this.dataView.byteLength) {

packages/typed-binary/src/io/measurer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Measurer implements IMeasurer {
2626
return this;
2727
}
2828

29-
fork() {
29+
fork(): IMeasurer {
3030
const forked = new Measurer();
3131
forked.size = this.size;
3232
return forked;

packages/typed-binary/src/structure/array.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<
8585
export function arrayOf<TSchema extends AnySchema>(
8686
elementSchema: TSchema,
8787
length: number,
88-
) {
88+
): ArraySchema<TSchema> {
8989
return new ArraySchema(elementSchema, length);
9090
}

packages/typed-binary/src/structure/baseTypes.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BoolSchema extends Schema<boolean> {
3030
}
3131
}
3232

33-
export const bool = new BoolSchema();
33+
export const bool: BoolSchema = new BoolSchema();
3434

3535
////
3636
// STRING
@@ -67,7 +67,7 @@ export class StringSchema extends Schema<string> {
6767
}
6868
}
6969

70-
export const string = new StringSchema();
70+
export const string: StringSchema = new StringSchema();
7171

7272
////
7373
// i8
@@ -97,7 +97,7 @@ export class Int8Schema extends Schema<number> {
9797
}
9898
}
9999

100-
export const i8 = new Int8Schema();
100+
export const i8: Int8Schema = new Int8Schema();
101101

102102
////
103103
// u8
@@ -127,12 +127,12 @@ export class Uint8Schema extends Schema<number> {
127127
}
128128
}
129129

130-
export const u8 = new Uint8Schema();
130+
export const u8: Uint8Schema = new Uint8Schema();
131131

132132
/**
133133
* Alias for `bin.u8`
134134
*/
135-
export const byte = u8;
135+
export const byte: Uint8Schema = u8;
136136

137137
////
138138
// i16
@@ -162,7 +162,7 @@ export class Int16Schema extends Schema<number> {
162162
}
163163
}
164164

165-
export const i16 = new Int16Schema();
165+
export const i16: Int16Schema = new Int16Schema();
166166

167167
////
168168
// u16
@@ -192,7 +192,7 @@ export class Uint16Schema extends Schema<number> {
192192
}
193193
}
194194

195-
export const u16 = new Uint16Schema();
195+
export const u16: Uint16Schema = new Uint16Schema();
196196

197197
////
198198
// i32
@@ -222,7 +222,7 @@ export class Int32Schema extends Schema<number> {
222222
}
223223
}
224224

225-
export const i32 = new Int32Schema();
225+
export const i32: Int32Schema = new Int32Schema();
226226

227227
////
228228
// u32
@@ -252,7 +252,7 @@ export class Uint32Schema extends Schema<number> {
252252
}
253253
}
254254

255-
export const u32 = new Uint32Schema();
255+
export const u32: Uint32Schema = new Uint32Schema();
256256

257257
////
258258
// f16
@@ -282,7 +282,7 @@ export class Float16Schema extends Schema<number> {
282282
}
283283
}
284284

285-
export const f16 = new Float16Schema();
285+
export const f16: Float16Schema = new Float16Schema();
286286

287287
////
288288
// f32
@@ -312,4 +312,4 @@ export class Float32Schema extends Schema<number> {
312312
}
313313
}
314314

315-
export const f32 = new Float32Schema();
315+
export const f32: Float32Schema = new Float32Schema();

packages/typed-binary/src/structure/chars.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { Measurer } from '../io/measurer.ts';
33
import type { IMeasurer, ISerialInput, ISerialOutput } from '../io/types.ts';
44
import { Schema } from './types.ts';
55

6-
export class CharsSchema extends Schema<string> {
7-
constructor(public readonly length: number) {
6+
export class CharsSchema<
7+
TLength extends number = number,
8+
> extends Schema<string> {
9+
constructor(public readonly length: TLength) {
810
super();
911
}
1012

@@ -35,6 +37,6 @@ export class CharsSchema extends Schema<string> {
3537
}
3638
}
3739

38-
export function chars<T extends number>(length: T) {
40+
export function chars<T extends number>(length: T): CharsSchema<T> {
3941
return new CharsSchema(length);
4042
}

packages/typed-binary/src/structure/dynamicArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ export class DynamicArraySchema<TElement extends AnySchema> extends Schema<
121121

122122
export function dynamicArrayOf<TSchema extends AnySchema>(
123123
elementSchema: TSchema,
124-
) {
124+
): DynamicArraySchema<TSchema> {
125125
return new DynamicArraySchema(elementSchema);
126126
}

packages/typed-binary/src/structure/keyed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,6 @@ export class KeyedSchema<
148148
export function keyed<K extends string, P extends ISchema<unknown>>(
149149
key: K,
150150
inner: (ref: ISchema<Ref<K>>) => P,
151-
) {
151+
): KeyedSchema<P, K> {
152152
return new KeyedSchema(key, inner);
153153
}

packages/typed-binary/src/structure/object.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ export class ObjectSchema<TProps extends Record<string, AnySchema>>
134134
}
135135
}
136136

137-
export const object = <P extends Record<string, AnySchema>>(properties: P) =>
138-
new ObjectSchema(properties);
137+
export function object<P extends Record<string, AnySchema>>(
138+
properties: P,
139+
): ObjectSchema<P> {
140+
return new ObjectSchema(properties);
141+
}
139142

140143
type UnwrapGeneric<Base extends Record<string, AnySchema>, Ext> = {
141144
[TKey in keyof Ext]: ISchema<
@@ -308,7 +311,7 @@ export function generic<
308311
S extends {
309312
[Key in keyof S]: AnySchemaWithProperties;
310313
},
311-
>(properties: P, subTypeMap: S) {
314+
>(properties: P, subTypeMap: S): GenericObjectSchema<P, S> {
312315
return new GenericObjectSchema(SubTypeKey.STRING, properties, subTypeMap);
313316
}
314317

@@ -317,6 +320,6 @@ export function genericEnum<
317320
S extends {
318321
[Key in keyof S]: AnySchemaWithProperties;
319322
},
320-
>(properties: P, subTypeMap: S) {
323+
>(properties: P, subTypeMap: S): GenericObjectSchema<P, S> {
321324
return new GenericObjectSchema(SubTypeKey.ENUM, properties, subTypeMap);
322325
}

0 commit comments

Comments
 (0)