mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Port message types to TypeScript
This commit is contained in:
1 parent
15ea9a91dc
commit
2a52a82fb7
31 files changed
+2846
-4124
No files matched your search
@@ -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");
|
||||
Reference in new issue
Block a user