/** * Copyright 2023 Fluence Labs Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ArrayType, ArrowType, LabeledProductType, NilType, OptionType, ScalarType, SimpleTypes, StructType, TopType, UnlabeledProductType, } from "@fluencelabs/interfaces"; import { Call, Pipe, Objects, Tuples, Unions, Fn } from "hotscript"; // Type definitions for inferring ts types from air json definition // In the future we may remove string type declaration and move to type inference. type GetTsTypeFromScalar = [T["name"]] extends [ "u8" | "u16" | "u32" | "u64" | "i8" | "i16" | "i32" | "i64" | "f32" | "f64", ] ? number : [T["name"]] extends ["bool"] ? boolean : [T["name"]] extends ["string"] ? string : never; type MapTuple = { [K in keyof T]: [T[K]] extends [SimpleTypes] ? GetSimpleType : never; }; type UnpackIfSingle = [T] extends [[infer R]] ? R : T; type GetSimpleType = [T] extends [NilType] ? null : [T] extends [ArrayType] ? GetSimpleType[] : [T] extends [StructType] ? { [K in keyof T["fields"]]: GetSimpleType } : [T] extends [OptionType] ? GetSimpleType | null : [T] extends [ScalarType] ? GetTsTypeFromScalar : [T] extends [TopType] ? unknown : never; interface Access extends Fn { return: __GetTsType, T>>; } type __GetTsType = [T] extends [SimpleTypes] ? GetSimpleType : [T] extends [UnlabeledProductType] ? MapTuple : [T] extends [LabeledProductType] ? { [K in keyof T["fields"]]: __GetTsType } : [T] extends [ArrowType] ? ( ...t: [H] extends [UnlabeledProductType] ? MapTuple : [H] extends [LabeledProductType] ? Pipe>]> : [] ) => [T["codomain"]] extends [UnlabeledProductType] ? UnpackIfSingle> : undefined : never; type DeepMutable = { -readonly [K in keyof T]: DeepMutable; }; export type GetTsType = __GetTsType>;