Typing improvement for `letrec` combinator
See original GitHub issue🚀 Feature Request
Improve typing of tie
function in letrec
combinator by expanding the type signature with the following, allowing it to read from the generic T
.
tie: {
<K extends keyof T>(key: K): Arbitrary<T[K]>;
(key: string): Arbitrary<unknown>;
}
// so the full signature becomes:
export declare function letrec<T>(
builder: (tie: {
<K extends keyof T>(key: K): Arbitrary<T[K]>;
(key: string): Arbitrary<unknown>;
}) => { [K in keyof T]: Arbitrary<T[K]> },
): { [K in keyof T]: Arbitrary<T[K]> };
Motivation
The way how tie
function of letrec
combinator is typed means that we have to explicitly cast result of each call to that function. This way it is only needed to specify all return types of all arbitraries in T
.
Example
For example, reusing letrec tree from docs.
import * as fc from 'fast-check';
const fst = fc.letrec((tie) => ({
tree: fc.oneof(tie('node'), tie('leaf'), tie('leaf')),
node: fc.record({
left: tie('tree'),
right: tie('tree'),
}),
leaf: fc.nat(),
}));
type Tree = number | { left: Tree; right: Tree };
const snd = fc.letrec<{
tree: Tree;
node: { left: Tree; right: Tree };
leaf: number;
}>((tie) => ({
tree: fc.oneof(tie('node'), tie('leaf'), tie('leaf')),
node: fc.record({
left: tie('tree'),
right: tie('tree'),
}),
leaf: fc.nat(),
}));
In the first example, without explicit type specification, it all still defaults to unknown. However, in the second example with explicit type specification, everything becomes well typed.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
TypingAcademy: Learn touch typing with our free typing practice
Learn to type with 10 fingers and practice your typing skills online. We offer everything you need to master touch typing with ease....
Read more >Learn To Type And Improve Typing Speed Free - Typing.com
Learn to touch type and improve your typing speed with free interactive typing lessons for all ages. Start your typing practice now!
Read more >printf in ocaml
Unfortunately, printf poses a problem for a strongly, statically typed language ... Female -> "Female";; let rec get_sex() = let str = print_string...
Read more >Typing.io: Typing Practice for Programmers - Hacker News
I have used this one before to practice typing on a split keyboard with chords. Never made it to proficiency but it worked...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Released in v3! Once again thanks a lot for the help on those typings
See comment on #1572, but I may have found a way to make it real for v3 🤞