Skip to content

Instantly share code, notes, and snippets.

@kdarty
Created August 15, 2018 15:25
Show Gist options
  • Save kdarty/d6ff1bd5ed2baf2b397837f6189b3d0b to your computer and use it in GitHub Desktop.
Save kdarty/d6ff1bd5ed2baf2b397837f6189b3d0b to your computer and use it in GitHub Desktop.
Here's a quick demo of using a set of Enums to derive a full Description for a Lookup value when only the Identifier was provided. Code is a copy/paste from TypeScript Playground for ease of testing
// String Enum Example using the TypeScript Playground example for test purposes
// Test here: https://www.typescriptlang.org/play/
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet(skip = false) {
let skipMessage = null;
if (skip) {
skipMessage = ' Skip';
}
return `${this.greeting}`;
}
}
// Status Type Identifiers (What we might get from an Api)
enum StatusTypeIds {
Submitted = 0,
Processing = 1,
OnHold = 2
}
// Status Type Descriptions
enum StatusTypes {
Submitted = 'Submitted',
Processing = 'Processing',
OnHold = 'On Hold for further investigation'
}
// Derive the StatusTypeId from the provided value
const statusTypeId = StatusTypeIds[2];
// Use the derived Enum value to retrieve the associated Description
const statusDescription = StatusTypes[statusTypeId];
let greeter = new Greeter(statusDescription);
let button = document.createElement('button');
button.textContent = "Status";
button.onclick = function() {
alert(greeter.greet(true));
}
document.body.appendChild(button);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment