Skip to content

Instantly share code, notes, and snippets.

@garronej
Created March 25, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garronej/2d5bd80c8c6d4cd45566426316a6c050 to your computer and use it in GitHub Desktop.
Save garronej/2d5bd80c8c6d4cd45566426316a6c050 to your computer and use it in GitHub Desktop.
            const typeGuard_= <U extends T, T>(o: T): o is U => true;

            const o: number =33;

            assert(typeGuard_<33, typeof o>(o));

            o; //<= o is 33

            //It would be nice to be able to do:

            assert(typeGuard_<33>(o));

            //...and have the compiler infer the type of T.

            //...and even nicer to be able to do

            const trustMe= <U extends T, T>(o: T): asserts o is U => {};

            trustMe<33>(o);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment