Skip to content

Commit

Permalink
Better type
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Nov 15, 2024
1 parent 60a72f2 commit 5e1613e
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions member/f32.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -13,7 +13,7 @@ import type { Struct } from '../struct.ts';
export function memberF32<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
4 changes: 2 additions & 2 deletions member/f64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -13,7 +13,7 @@ import type { Struct } from '../struct.ts';
export function memberF64<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
6 changes: 3 additions & 3 deletions member/i16.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -13,7 +13,7 @@ import type { Struct } from '../struct.ts';
export function memberI16<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function memberI16<T extends typeof Struct>(
export function memberU16<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
6 changes: 3 additions & 3 deletions member/i24.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';
import { getInt24, getUint24, setInt24, setUint24 } from '../util.ts';

Expand All @@ -14,7 +14,7 @@ import { getInt24, getUint24, setInt24, setUint24 } from '../util.ts';
export function memberI24<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function memberI24<T extends typeof Struct>(
export function memberU24<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
6 changes: 3 additions & 3 deletions member/i32.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -13,7 +13,7 @@ import type { Struct } from '../struct.ts';
export function memberI32<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function memberI32<T extends typeof Struct>(
export function memberU32<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
6 changes: 3 additions & 3 deletions member/i64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType } from '../type.ts';
import type { KeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -13,7 +13,7 @@ import type { Struct } from '../struct.ts';
export function memberI64<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], bigint>,
member: KeyofExtends<T['prototype'], bigint>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function memberI64<T extends typeof Struct>(
export function memberU64<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], bigint>,
member: KeyofExtends<T['prototype'], bigint>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
10 changes: 5 additions & 5 deletions member/i8.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyofType, ReadonlyKeyofType } from '../type.ts';
import type { KeyofExtends, ReadonlyKeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -12,7 +12,7 @@ import type { Struct } from '../struct.ts';
export function memberI8<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
): number {
Object.defineProperty(StructT.prototype, member, {
get(this: T['prototype']): number {
Expand All @@ -36,7 +36,7 @@ export function memberI8<T extends typeof Struct>(
export function memberU8<T extends typeof Struct>(
StructT: T,
offset: number,
member: KeyofType<T['prototype'], number>,
member: KeyofExtends<T['prototype'], number>,
): number {
Object.defineProperty(StructT.prototype, member, {
get(this: T['prototype']): number {
Expand All @@ -61,7 +61,7 @@ export function memberU8<T extends typeof Struct>(
export function memberI8A<T extends typeof Struct>(
StructT: T,
offset: number,
member: ReadonlyKeyofType<T['prototype'], Int8Array>,
member: ReadonlyKeyofExtends<T['prototype'], Int8Array>,
count: number,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand All @@ -84,7 +84,7 @@ export function memberI8A<T extends typeof Struct>(
export function memberU8A<T extends typeof Struct>(
StructT: T,
offset: number,
member: ReadonlyKeyofType<T['prototype'], Uint8Array>,
member: ReadonlyKeyofExtends<T['prototype'], Uint8Array>,
count: number,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
4 changes: 2 additions & 2 deletions member/struct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ReadonlyKeyofType } from '../type.ts';
import type { ReadonlyKeyofExtends } from '../type.ts';
import type { Struct } from '../struct.ts';

/**
Expand All @@ -14,7 +14,7 @@ export function memberStruct<M extends typeof Struct, T extends typeof Struct>(
StructM: M,
StructT: T,
offset: number,
member: ReadonlyKeyofType<T['prototype'], M['prototype']>,
member: ReadonlyKeyofExtends<T['prototype'], M['prototype']>,
littleEndian: boolean | null = null,
): number {
Object.defineProperty(StructT.prototype, member, {
Expand Down
12 changes: 6 additions & 6 deletions type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export type ReadonlyKeyof<T> = NonNullable<
>;

/**
* Keys for a given value type.
* Keys of a type that another type extends.
*/
export type KeyofType<T, U> = {
[K in keyof T]: T[K] extends U ? K : never;
export type KeyofExtends<T, E> = {
[K in keyof T]: E extends T[K] ? K : never;
}[keyof T];

/**
* Readonly keys for a given value type.
* Readonly keys of a type that another type extends.
*/
export type ReadonlyKeyofType<T, U> = {
[K in ReadonlyKeyof<T>]: T[K] extends U ? K : never;
export type ReadonlyKeyofExtends<T, E> = {
[K in ReadonlyKeyof<T>]: E extends T[K] ? K : never;
}[ReadonlyKeyof<T>];

/**
Expand Down

0 comments on commit 5e1613e

Please sign in to comment.