Created
July 4, 2018 11:23
-
-
Save asiniy/224944352d32672fb5d520ea77a3ec7b to your computer and use it in GitHub Desktop.
index.js // Extending JavaScript with ease (beginner level tutorial)
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
// The only significant difference between TS and JavaScript is interface. | |
// Interface declarates that object passed as `obj` should be an object actually | |
interface PassedObject { | |
readonly [key: string]: any; | |
} | |
// Same logic as in our ES6 code | |
const objectFetch = (obj: PassedObject, property: string): any => { | |
// tslint:disable-next-line:no-if-statement | |
if (obj.hasOwnProperty(property)) { | |
return obj[property]; | |
} | |
throw new ReferenceError(`No such property: ${property}`); | |
}; | |
export default objectFetch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment