Skip to content

Commit

Permalink
allow recursive types to be any by default
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Oct 1, 2023
1 parent b4cdbcb commit d8a8a5e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/recursion/src/recursion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import MyFullCanister from '../recursive_canister';
// These are the types that can be recursive
// Record
// Record can't be recursive by itself. It needs something to be able to terminate it. It needs to work with Variants, Opts, and Vec
const varRecord = Recursive(() => Record({ myVar: myVar }));
const varRecord = Recursive(() => Record({ myVar }));
const vecRecord = Recursive(() => Record({ myVecRecords: Vec(vecRecord) }));
const optRecord = Recursive(() => Record({ myOpt: Opt(optRecord) }));
const myVar = Variant({ num: int8, varRec: varRecord });
Expand All @@ -33,7 +33,7 @@ const recVariant = Recursive(() =>
const optTuple = Recursive(() => Tuple(Opt(optTuple), Opt(optTuple)));
const vecTuple = Recursive(() => Tuple(Vec(vecTuple), Vec(vecTuple)));
const varTuple = Recursive(() => Tuple(myTupleVar, myTupleVar));
const myTupleVar = Variant({ num: int8, varTuple: varTuple });
const myTupleVar = Variant({ num: int8, varTuple });
// Vec
// Vec can't be recursive by itself. At the end of it all it needs to have a concrete type.
// Opt
Expand Down
2 changes: 1 addition & 1 deletion src/lib_functional/candid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type TypeMapping<T> = T extends () => any
: T extends AzleVoid
? void
: T extends AzleTuple<infer U>
? { [K in keyof U]: TypeMapping<U[K]> }
? { [K in keyof U]: U[K] extends any ? any : TypeMapping<U[K]> }
: T extends AzleVec<infer U>
? TypeMapping<U>[]
: T extends AzleOpt<infer U>
Expand Down
2 changes: 1 addition & 1 deletion src/lib_functional/candid/reference/recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v4 } from 'uuid';
import { IDL } from '@dfinity/candid';
import { Parent } from '../../../lib_new/utils';

export function Recursive<T extends () => any>(idlCallback: T): T {
export function Recursive(idlCallback: any): any {
const name = v4();

let result = (...args: any[]) => {
Expand Down
7 changes: 5 additions & 2 deletions src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IDL } from '../../lib_new/index';
import { ic } from '../../lib_new/ic';
import { TypeMapping } from '..';
import { CandidType, TypeMapping } from '..';
import {
DecodeVisitor,
EncodeVisitor
Expand Down Expand Up @@ -33,7 +33,10 @@ export type CanisterMethodInfo<T extends ReadonlyArray<any>, K> = {
guard: (() => any) | undefined;
};

export type Callback<Params extends ReadonlyArray<any>, Return> = (
export type Callback<
Params extends ReadonlyArray<CandidType>,
Return extends CandidType
> = (
...args: { [K in keyof Params]: TypeMapping<Params[K]> }
) => TypeMapping<Return> | Promise<TypeMapping<Return>>;

Expand Down
2 changes: 1 addition & 1 deletion src/lib_functional/canister_methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createParents,
executeMethod
} from '.';
import { CandidType, RecursiveType, TypeMapping } from '../candid';
import { CandidType, TypeMapping } from '../candid';
import { toParamIDLTypes, toReturnIDLType } from '../../lib_new/utils';

export function query<
Expand Down

0 comments on commit d8a8a5e

Please sign in to comment.