Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active May 21, 2018 16:13
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 dex4er/0cbac3a47b953775337eb0d82299ce5c to your computer and use it in GitHub Desktop.
Save dex4er/0cbac3a47b953775337eb0d82299ce5c to your computer and use it in GitHub Desktop.
Example from https://reasonml.github.io/ as Typescript
class Teacher {
}
class Director {
}
class Student {
constructor(name) {
this.name = name;
}
}
function greeting(stranger) {
if (stranger instanceof Teacher)
return "Hey professor!";
if (stranger instanceof Director)
return "Hello director.";
if (stranger instanceof Student) {
if (stranger.name === "Richard")
return "Still here Ricky?";
else
return "Hey, " + stranger.name + ".";
}
}
class Teacher {}
class Director {}
class Student {
constructor (public name: string) {}
}
type SchoolPerson = Teacher | Director | Student
function greeting (stranger: SchoolPerson): string | void {
if (stranger instanceof Teacher) return "Hey professor!"
if (stranger instanceof Director) return "Hello director."
if (stranger instanceof Student) {
if (stranger.name === "Richard") return "Still here Ricky?"
else return "Hey, " + stranger.name + "."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment