Created
June 12, 2019 22:53
-
-
Save ASafaeirad/591cb58a9c17cc3605ee65e1f09ac60b to your computer and use it in GitHub Desktop.
Javascript dynamic type system decorator
This file contains hidden or 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
| const typeDecorator = (type) => | |
| (target, key, descriptor) => { | |
| let value; | |
| return { | |
| enumerable: true, | |
| get: function () { return value; }, | |
| set: function (newValue) { | |
| const newType = typeof newValue; | |
| if (newType !== type || newType === 'undefined') { | |
| const error = `invalid type for "${key}", expect "${type}" but get "${newType}"`; | |
| throw Error(error); | |
| } | |
| value = newValue; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment