Port message types to TypeScript

This commit is contained in:
Hornwitser committed 2023-08-11 00:24:00 +02:00
1 parent 15ea9a91dc
commit 2a52a82fb7
31 files changed
+2846 -4124

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
import { Type } from "@sinclair/typebox";
export function StringEnum<T extends string[]>(values: [...T]) {
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
}
export interface JSONDeserialisable<T> {
fromJSON(json: unknown): T;
jsonSchema: object;
}
export function jsonArray<T>(ItemClass: JSONDeserialisable<T>) {
return {
jsonSchema: {
type: "array",
items: ItemClass.jsonSchema,
},
fromJSON(json: Array<T>) {
return json.map(i => ItemClass.fromJSON(i));
},
};
}
export function jsonPrimitive<T, U>(type: U) {
return {
jsonSchema: { type },
fromJSON(json: T) {
return json;
},
};
}
export const JsonBoolean = jsonPrimitive<boolean, "boolean">("boolean");
export const JsonNumber = jsonPrimitive<number, "number">("number");
export const JsonString = jsonPrimitive<string, "string">("string");