-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: override and readonly decorators deprecate and replaced with a …
…`prop` decorator
- Loading branch information
Showing
8 changed files
with
87 additions
and
105 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** Property configuration */ | ||
export type OverrideDecoratorConfig = { | ||
/** Value to set in the prototype */ | ||
value?: any; | ||
/** To define mutable property */ | ||
readonly?: false; | ||
} | { | ||
/** Value to set in the prototype */ | ||
value: any; | ||
/** To define readonly property */ | ||
readonly: true; | ||
}; | ||
|
||
/** | ||
* `@prop` is auxiliary decorator to define a field on the prototype level. | ||
*` @prop` can be used to override decorated property from the parent level | ||
* | ||
* You can also use an @override decorator in combination with ECMA Script class property definition: | ||
* `@prop() public field: any = initial value;` | ||
* | ||
* The class property initial value is a part of object creation, so it goes to the object itself, | ||
* while the @override value is defined on the prototype level. | ||
* | ||
* @param [prototypeConfig] - prototype property configuration | ||
*/ | ||
export function prop(prototypeConfig: OverrideDecoratorConfig = {}) { | ||
return function (obj: any, name: string): void { | ||
if (Object.hasOwnProperty.call(obj, name)) { | ||
throw new TypeError('Can\'t override own property'); | ||
} | ||
Object.defineProperty(obj, name, { | ||
value: prototypeConfig.value, | ||
writable: !prototypeConfig.readonly, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
}; | ||
} |
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
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
This file was deleted.
Oops, something went wrong.