Skip to content

Instantly share code, notes, and snippets.

@jlherren
Created February 5, 2023 17:37
Show Gist options
  • Save jlherren/149af87aca13a5eabeab0a1092f5c9b2 to your computer and use it in GitHub Desktop.
Save jlherren/149af87aca13a5eabeab0a1092f5c9b2 to your computer and use it in GitHub Desktop.
type DeliminatedDocument = {
text: string;
separator: "comma" | "tab";
}
type PlaintextDocument = {
text: string;
}
type WeirdPlaintextDocument = PlaintextDocument & {
separator: 'lol';
}
const printDeliminated = (doc: DeliminatedDocument) => {
console.log('Printing deliminitated document with separator', doc.separator);
}
const printPlaintext = (doc: PlaintextDocument) => {
console.log('Printing plaintext document');
}
const printDocument = (doc: DeliminatedDocument | PlaintextDocument) => {
if ("separator" in doc) {
printDeliminated(doc);
} else {
printPlaintext(doc);
}
}
const handleWeirdPlaintextDocument = (doc: WeirdPlaintextDocument) => {
printDocument(doc);
}
handleWeirdPlaintextDocument({
text: 'I am plaintext',
separator: 'lol',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment