Skip to content

Instantly share code, notes, and snippets.

@dex4er

dex4er/schoolPerson.js

Last active May 21, 2018
Embed
What would you like to do?
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