Created
August 27, 2021 10:17
-
-
Save eamonnboyle/f2065c10db9222f7eb2bebd28ed04a03 to your computer and use it in GitHub Desktop.
Nested Property Guards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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