Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active October 9, 2018 22:23
Show Gist options
  • Select an option

  • Save dewey92/51268f66cedeaa7d083773f67134fe29 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/51268f66cedeaa7d083773f67134fe29 to your computer and use it in GitHub Desktop.
Intersection & Union Type
/* Intersection type */
interface Goku {
withKamehameha: boolean;
}
interface Bejita {
sayKakarot: boolean;
}
type Gogeta = Goku & Bejita;
const gogeta: Gogeta = {
withKamehameha: true,
sayKakarot: true,
}; // ✅
/* Union Type */
type DateOrString = Date | string;
const isSunday = (date: DateOrString) =>
(new Date(date)).getDay() === 0;
isSunday('2018-10-08'); // ✅
isSunday(new Date('2018-10-08')); // ✅
isSunday(999); // ❌
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment