Skip to content

Instantly share code, notes, and snippets.

@ifiokjr
Last active November 13, 2018 04:26
Show Gist options
  • Save ifiokjr/c7d7c6ea40ecd6ec16fa2ef0bb7b2706 to your computer and use it in GitHub Desktop.
Save ifiokjr/c7d7c6ea40ecd6ec16fa2ef0bb7b2706 to your computer and use it in GitHub Desktop.
TS code samples
/**
* Jest tests can be destructured. However it won't work with inlineSnapshots
*/
const thrower = () => {
throw new Error('test');
};
const simple = () => {
//
};
test('#baseDefinition', () => {
let t = true;
const { not, toThrow } = expect(() => (t ? thrower() : simple()));
toThrow();
t = false;
not.toThrow();
});
// Replace return type
/**
* Helper for retrieving the argument type of a function
*/
export type ArgumentTypes<T> = T extends (...args: Array<infer U>) => any ? U : never;
/**
* Replace that argument type
*/
type ReplaceReturnType<T, TNewReturn> = (...a: ArgumentTypes<T>) => TNewReturn;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment