Skip to content

Instantly share code, notes, and snippets.

@guayom
Last active June 6, 2020 17:58
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 guayom/56ab7f946438f8f30945f1bf68060b3c to your computer and use it in GitHub Desktop.
Save guayom/56ab7f946438f8f30945f1bf68060b3c to your computer and use it in GitHub Desktop.
Typescript patterns
// Use the Javascript "in" operator for automatic type inference
// https://egghead.io/lessons/typescript-use-the-javascript-in-operator-for-automatic-type-inference-in-typescript
interface Admin {
id: string;
role: string;
}
interface User {
email: string;
}
function redirect(user: Admin | User) {
if ("role" in user) {
// The type is Admin, because it has a property "role"
console.log(user.role);
} else {
console.log(user.email);
}
}
// https://github.com/eggheadio-projects/practical-advanced-typescript-features/tree/4-infer-type-switch-statement
interface Action {
type: string;
}
const AddPerson = {
type = "Add",
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment