question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript typings

See original GitHub issue

I created a pull request in the DefinitelyTyped repository: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/31953

It’s basically working, but some things could be nicer (or better typed):

  1. Generics for Spec and Result
  2. Typings if SpecValue is a function

Especially point 2 is hard for me to figure out…

Example(s):

const spec = {
    a: [[ values => R.all((val) => false, values), 'msg']],
    b: R.map(R.always([[(val) => false, 'msg']])),
    c: R.map(() => [[(val) => false, 'msg']])
}

I have no clue what those function signatures for a, b and c are. I know from your source code, that those functions have a single argument (= value). But I have no clue about the return types.

I’m no Ramda / Functional Programming export. Though some help would be appreciated 😄 Especially when it comes to currying I’m lost 😆

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:31

github_iconTop GitHub Comments

1reaction
benneqcommented, Jan 18, 2019

Thanks to jack-williams we now have working booleans! https://github.com/Microsoft/TypeScript/issues/29477

I didn’t know about this “distributive conditional types” in TypeScript. Really weird stuff 😆

declare function spected<ROOTINPUT, SPEC extends SpecValue<ROOTINPUT, ROOTINPUT> = SpecValue<ROOTINPUT, ROOTINPUT>>(spec: SPEC, input: ROOTINPUT): Result<ROOTINPUT, SPEC>;

type Predicate<INPUT, ROOTINPUT> = (value: INPUT, inputs: ROOTINPUT) => boolean;

type ErrorMsg<INPUT> =
    | (string | number | boolean | symbol | null | undefined | object)
    | ((value: INPUT, field: string) => any);

export type Spec<INPUT, ROOTINPUT = any> = [Predicate<INPUT, ROOTINPUT>, ErrorMsg<INPUT>];

export type SpecArray<INPUT, ROOTINPUT = any> = Array<Spec<INPUT, ROOTINPUT>>

export type SpecFunction<INPUT, ROOTINPUT = any> = [INPUT] extends [ReadonlyArray<infer U>]
    ? (value: INPUT) => ReadonlyArray<SpecArray<U, ROOTINPUT>>
    : [INPUT] extends [object]
        ? (value: INPUT) => SpecObject<INPUT, ROOTINPUT>
        : (value: INPUT) => SpecArray<INPUT, ROOTINPUT>;

export type SpecObject<INPUT, ROOTINPUT = any> = Partial<{[key in keyof INPUT]: SpecValue<INPUT[key], ROOTINPUT>}>

export type SpecValue<INPUT, ROOTINPUT = any> = [INPUT] extends [ReadonlyArray<any>]
    ? SpecArray<INPUT, ROOTINPUT> | SpecFunction<INPUT, ROOTINPUT>
        : [INPUT] extends [object]
            ? SpecArray<INPUT, ROOTINPUT> | SpecFunction<INPUT, ROOTINPUT> | SpecObject<INPUT, ROOTINPUT>
            : SpecArray<INPUT, ROOTINPUT> | SpecFunction<INPUT, ROOTINPUT>;

export type Result<INPUT, SPEC> = {[key in keyof INPUT]: true | any[] | Result<INPUT[key], any>};
1reaction
benneqcommented, Jan 17, 2019

Yes this works. But again there are problems with type inference for tuples 😦

const data = {
    b: ["a", "b"]
}
  
const rules: Spec<typeof data, typeof data> = {
    b: input => input.map(val => [[(val) => false, 'msg']])
    // here [[(val) => false, 'msg']] is not detected as tuple [Predicate, ErrorMsg],
    // but instead as Array<Predicate | ErrorMsg>
    // so you've got to write this:
    // input => input.map(val => [[(val) => false, 'msg']] as [Predicate<string, any>, ErrorMsg<string>][])
}

And the other problem is, that you are allowed to write (val) => (val) => (val) => ......

Read more comments on GitHub >

github_iconTop Results From Across the Web

typings/typings: *DEPRECATED* The TypeScript Definition ...
Typings is the simple way to manage and install TypeScript definitions. It uses typings.json , which can resolve to the Typings Registry, GitHub,...
Read more >
Documentation - Type Declarations - TypeScript
TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and ...
Read more >
Typescript Typings: The Complete Guide: @types Compiler ...
Typescript Typings - The Complete Guide To Type Definitions: @types, Compiler Opt-In Types: When To Use Each and Why?
Read more >
Types of TypeScript typings - Medium
A brief overview of the different approaches TypeScript offers to make our code type safe, in the context of React.
Read more >
Dynamic Static Typing In TypeScript - Smashing Magazine
In this article, we look at some of the more advanced features of TypeScript, like union types, conditional types, template literal types, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found