Skip to content

Instantly share code, notes, and snippets.

@blemoine
Last active March 31, 2018 20:39
Show Gist options
  • Save blemoine/099d2255fe861b98cbdc2674902ffff3 to your computer and use it in GitHub Desktop.
Save blemoine/099d2255fe861b98cbdc2674902ffff3 to your computer and use it in GitHub Desktop.
function executeLater(callback: () => void, timeout: PositiveNumber) {
setTimeout(callback, timeout); //no need to unwrap, timeout is still a `number`
}
const timeout: number = 100;
const callback = () => console.log('Execution!')
executeLater(callback, timeout) //Argument of type 'number' is not assignable to parameter of type 'PositiveNumber'.
// TypeScript doesn't know that `100` is a `PositiveNumber`
if (isPositiveNumber(timeout)) {
executeLater(callback, timeout) //compile and works !
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment