From 389e19c1032c388bc6a604f307ed38f0b84ac5ce Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 27 Sep 2023 16:33:12 -0500 Subject: [PATCH] add type inference for functions recursive types and for Tuples --- canisters/ledger/index.ts | 5 ++--- canisters/management/index.ts | 4 ++-- examples/complex_init/src/index.ts | 1 - src/lib_functional/candid/index.ts | 14 ++++++-------- src/lib_new/primitives.ts | 4 ++-- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/canisters/ledger/index.ts b/canisters/ledger/index.ts index 9c2079d61d..bc72dfe770 100644 --- a/canisters/ledger/index.ts +++ b/canisters/ledger/index.ts @@ -8,15 +8,14 @@ import { blob, + Canister, nat, nat8, nat32, nat64, Null, Opt, - Principal, Record, - Service, query, update, text, @@ -298,7 +297,7 @@ export const DecimalsResult = Record({ export type Address = text; export const Address = text; -export const Ledger = Service({ +export const Ledger = Canister({ // Transfers tokens from a subaccount of the caller to the destination address. // The source address is computed from the principal of the caller and the specified subaccount. // When successful, returns the index of the block containing the transaction. diff --git a/canisters/management/index.ts b/canisters/management/index.ts index bb91f40189..4202551d9c 100644 --- a/canisters/management/index.ts +++ b/canisters/management/index.ts @@ -1,7 +1,7 @@ import { blob, + Canister, Principal, - Service, update, Vec, Void @@ -44,7 +44,7 @@ export * from './canister_management'; export * from './http_request'; export * from './t_ecdsa'; -export const managementCanister = Service({ +export const managementCanister = Canister({ // bitcoin bitcoin_get_balance: update([GetBalanceArgs], Satoshi), bitcoin_get_current_fee_percentiles: update( diff --git a/examples/complex_init/src/index.ts b/examples/complex_init/src/index.ts index bdb25328f5..f1dc9f6126 100644 --- a/examples/complex_init/src/index.ts +++ b/examples/complex_init/src/index.ts @@ -7,7 +7,6 @@ const User = Record({ let greeting: text = 'Hello User'; let user: Opt = []; -// TODO tuple types aren't done, they don't have TypeScript types export default Canister({ init: init([Tuple(text, User)], (tuple) => { greeting = tuple[0]; diff --git a/src/lib_functional/candid/index.ts b/src/lib_functional/candid/index.ts index adb5c74671..868d6e968e 100644 --- a/src/lib_functional/candid/index.ts +++ b/src/lib_functional/candid/index.ts @@ -45,7 +45,9 @@ import { Tuple } from '../../lib_new'; -export type TypeMapping = T extends IDL.TextClass +export type TypeMapping = T extends () => any + ? ReturnType + : T extends IDL.TextClass ? string : T extends AzleBool ? bool @@ -75,13 +77,9 @@ export type TypeMapping = T extends IDL.TextClass ? float32 : T extends never[] ? void - : T extends AzleTuple - ? any - : // : T extends AzleTuple<[infer U]> - // ? [TypeMapping] - // : T extends AzleTuple<[infer U, infer W]> - // ? [TypeMapping, TypeMapping] - T extends AzleVec + : T extends AzleTuple + ? { [K in keyof U]: TypeMapping } + : T extends AzleVec ? TypeMapping[] : T extends AzleOpt ? [TypeMapping] | [] diff --git a/src/lib_new/primitives.ts b/src/lib_new/primitives.ts index 73293015cc..f9dcf8bcb0 100644 --- a/src/lib_new/primitives.ts +++ b/src/lib_new/primitives.ts @@ -214,7 +214,7 @@ export class AzleVec { } } -export class AzleTuple { +export class AzleTuple { constructor(t: CandidClass[]) { this._azleTypes = t; } @@ -233,7 +233,7 @@ export function Vec(t: T): AzleVec { } // TODO I am not sure of any of these types... but its working so... -export function Tuple(...types: any[]): AzleTuple { +export function Tuple(...types: T): AzleTuple { // const idlTypes = types.map((value) => { // return toIDLType(value); // });