Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
Created August 27, 2021 10:17
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 eamonnboyle/f2065c10db9222f7eb2bebd28ed04a03 to your computer and use it in GitHub Desktop.
Save eamonnboyle/f2065c10db9222f7eb2bebd28ed04a03 to your computer and use it in GitHub Desktop.
Nested Property Guards
interface InputArea {
control: HTMLTextAreaElement | HTMLInputElement;
}
interface Component {
inputArea: InputArea;
}
function setupComponent(component: Component) {
if ('rows' in component.inputArea.control) {
// These properties are only valid for HTMLTextAreaElement
component.inputArea.control.rows = 25;
component.inputArea.control.cols = 80;
component.inputArea.control.wrap = 'hard';
} else {
// These properties are only valid for HTMLInputElement
component.inputArea.control.width = 400;
component.inputArea.control.height = 50;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment