Skip to content

Commit

Permalink
add type inference for functions recursive types and for Tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 27, 2023
1 parent 44e316c commit 389e19c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
5 changes: 2 additions & 3 deletions canisters/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

import {
blob,
Canister,
nat,
nat8,
nat32,
nat64,
Null,
Opt,
Principal,
Record,
Service,
query,
update,
text,
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions canisters/management/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
blob,
Canister,
Principal,
Service,
update,
Vec,
Void
Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion examples/complex_init/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const User = Record({
let greeting: text = 'Hello User';
let user: Opt<typeof User> = [];

// TODO tuple types aren't done, they don't have TypeScript types
export default Canister({
init: init([Tuple(text, User)], (tuple) => {
greeting = tuple[0];
Expand Down
14 changes: 6 additions & 8 deletions src/lib_functional/candid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import {
Tuple
} from '../../lib_new';

export type TypeMapping<T> = T extends IDL.TextClass
export type TypeMapping<T> = T extends () => any
? ReturnType<T>
: T extends IDL.TextClass
? string
: T extends AzleBool
? bool
Expand Down Expand Up @@ -75,13 +77,9 @@ export type TypeMapping<T> = T extends IDL.TextClass
? float32
: T extends never[]
? void
: T extends AzleTuple
? any
: // : T extends AzleTuple<[infer U]>
// ? [TypeMapping<U>]
// : T extends AzleTuple<[infer U, infer W]>
// ? [TypeMapping<U>, TypeMapping<W>]
T extends AzleVec<infer U>
: T extends AzleTuple<infer U>
? { [K in keyof U]: TypeMapping<U[K]> }
: T extends AzleVec<infer U>
? TypeMapping<U>[]
: T extends AzleOpt<infer U>
? [TypeMapping<U>] | []
Expand Down
4 changes: 2 additions & 2 deletions src/lib_new/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class AzleVec<T> {
}
}

export class AzleTuple {
export class AzleTuple<T extends any[]> {
constructor(t: CandidClass[]) {
this._azleTypes = t;
}
Expand All @@ -233,7 +233,7 @@ export function Vec<T>(t: T): AzleVec<T> {
}

// TODO I am not sure of any of these types... but its working so...
export function Tuple(...types: any[]): AzleTuple {
export function Tuple<T extends any[]>(...types: T): AzleTuple<T> {
// const idlTypes = types.map((value) => {
// return toIDLType(value);
// });
Expand Down

0 comments on commit 389e19c

Please sign in to comment.