Skip to content

Commit

Permalink
make t.recursion slightly more type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jan 23, 2018
1 parent ddd2534 commit 1614eac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,16 @@ export class RecursiveType<RT extends Any, A = any, O = A, I = mixed> extends Ty
}
}

export const recursion = <A, RT extends Mixed = Mixed, O = A, I = mixed>(
export const recursion = <A, O = mixed, I = mixed, RT extends Type<A, O, I> = Type<A, O, I>>(
name: string,
definition: (self: RT) => RT
): RecursiveType<RT, A, O, I> => {
const Self: any = new RecursiveType(name, (m): m is A => type.is(m), (m, c) => type.validate(m, c), identity)
const Self: any = new RecursiveType<RT, A, O, I>(
name,
(m): m is A => type.is(m),
(m, c) => type.validate(m, c),
identity as any
)
const type = definition(Self)
Self.type = type
Self.serialize = type.serialize
Expand Down
8 changes: 4 additions & 4 deletions typings-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ type Rec = {
b: Rec | undefined
}

const Rec = t.recursion<Rec, Generable>('T', self =>
t.interface({
const RecRT = t.recursion<Rec, Rec, t.mixed, GenerableInterface>('T', self => {
return t.interface({
a: t.number,
b: t.union([self, t.undefined])
})
)
})

f(Rec) // OK!
f(RecRT) // OK!

0 comments on commit 1614eac

Please sign in to comment.