News for hackers. Stuff that matters.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by nicoburns on Thursday September 12, 2019 @05:30PM (#20956678)
The "Assert signatures" feature looks excellent. The biggest issue with typescript has always been validating external inputs actually meet the type contracts that your code says they do. With this, it looks like this could be written as library!
Re:TypeScript 3.7: The Biggest Features and How to Use Them by joshAg on Thursday September 12, 2019 @05:38PM (#20956752)
How do you verify it now? I've been using typeguards, but since i haven't found a good way to generate interfaces from typeguard functions or typeguard functions from interfaces I have to do each manually:

  interface ContactInfo {
    address_1: string;
    address_2?: string;
    city: string;
    state: string;
    zip: string;
    phone: number;
  }
  function isContactInfo(arg: any): arg is ContactInfo {
    let valid = false;
    const contactInfoFields: { readonly [key: string]: string[] } = {
      address_1: [ 'string', ],
      address_2: [ 'string', 'undefined' ],
      city: [ 'string', ],
      state: [ 'string', ],
      zip: [ 'string', ],
      phone: [ 'number' ],
    };

    if (typeof arg !== 'object') {
      return valid;
    }
    for (const key in contactInfoFields) {
      if(!contactInfoFields[key].includes(typeof arg[key]) || arg[key] === '' || Number.isNaN(arg[key])) {
        return valid;
      }
    }
    valid = true;
    return valid;
  }
Re:TypeScript 3.7: The Biggest Features and How to Use Them by frankpf on Thursday September 12, 2019 @05:48PM (#20956835)
Not related to the new assert signatures feature (which is great!), but IMO the two best approaches to do what you want today are:

- io-ts[1]

This requires you to write your types as a runtime value, and allows you to extract static types from those, e.g.:

    const ContactInfo = t.type({
       address_1: t.string,
       ...
    })

    type ContactInfo = t.TypeOf<typeof ContactInfo>
You can validate objects with `ContactInfo.decode(someObject)`

Note that we can have the same name for the type and value because they live in different namespaces. When you do ContactInfo.decode, you're calling the decode property of `const ContactInfo`. When you use `ContactInfo` in a type position (e.g. `function x(arg: ContactInfo)`), you're using the `type ContactInfo` declaration

- typescript-is[2]

This uses TypeScript's transformer API. You can use it with https://github.com/cevek/ttypescript. It generates validators for your interfaces/types/etc at compile-time.

[1]: https://github.com/gcanti/io-ts

[2]: https://github.com/woutervh-/typescript-is

Re:TypeScript 3.7: The Biggest Features and How to Use Them by joshAg on Thursday September 12, 2019 @06:01PM (#20956935)
typescript-is looks like it can be a drop-in solution since we're only dealing with PODS. Thanks!
Re:TypeScript 3.7: The Biggest Features and How to Use Them by jakelazaroff on Thursday September 12, 2019 @10:27PM (#20958610)
You can extract a type from a typeguard using a conditional type!

  type TypeOf<T> = (x: any) => x is (infer U) ? U : never;
I wrote a really small and simple validator library called narrows that works this way [1]. You could simplify your code like so:

  import { number, optional, record, string, TypeOf } from 'narrows';

  const isContactInfo = record({
    address_1: string,
    address_2: optional(string),
    city: string,
    state: string,
    zip: string,
    phone: number
  })
  
  type ContactInfo = TypeOf<typeof isContactInfo>;
The source is less than 50 lines and super readable if you want to dig in. The libraries in the sibling comments are great too!

[1] https://www.npmjs.com/package/narrows

Re:TypeScript 3.7: The Biggest Features and How to Use Them by joshAg on Friday September 13, 2019 @01:54PM (#20964540)
I've been hesitant to switch to something that demands custom type declarations, because we're already autogenerating a lot of types/interfaces with express-openapi (we lean heavily on their request and response validators, too but those don't cover all the types we make), and getting that to play nicely with custom declarations is not my idea of fun. But for the types that aren't tied back to an json-endpoint (we have a few endpoints where we're getting xml or csv or xlsx and a few db<-->backend only interfaces), this looks like a great solution.

Typescript-is looks like it might have an overall edge, though, because we can use it on the autogenerated types as well with almost no hassle.

Re:TypeScript 3.7: The Biggest Features and How to Use Them by alangpierce on Thursday September 12, 2019 @07:42PM (#20957660)
In addition to the suggestions in the sibling comments, I've had good luck with ts-interface-checker/ts-interface-builder . You add a code generation step to your build process (rather than ttsc) that generates runtime equivalents of your TS types, and there's a library to perform a runtime check.

https://github.com/gristlabs/ts-interface-checker

Re:TypeScript 3.7: The Biggest Features and How to Use Them by moltar on Thursday September 12, 2019 @05:50PM (#20956846)
Check runtypes module.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by _bxg1 on Thursday September 12, 2019 @06:32PM (#20957178)
Definitely. Using Flow, type refinement has been one of my biggest annoyances, because the list of things the type system recognizes as such is hardcoded and can't be extended:

  instanceof
  typeof
  Array.isArray()
  == null
And if you want to refine object types it gets even worse; you have to just check for properties that may or may not be there, which means those properties have to be rigidly distinguishable between object types in a union, etc. etc. Being able to do custom, rich object validation and give it first-class hooks into the type system sounds incredible.