Skip to content

Instantly share code, notes, and snippets.

@phenomnomnominal
Created May 29, 2019 19:24
Show Gist options
  • Save phenomnomnominal/a6bc59eea954176e1a90e11ab61a18d9 to your computer and use it in GitHub Desktop.
Save phenomnomnominal/a6bc59eea954176e1a90e11ab61a18d9 to your computer and use it in GitHub Desktop.
A decorator that fixes boolean input values
const VALUE_KEY = '__value_';
export function isBool (): PropertyDecorator {
return (target: any, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
get: function () {
return this[`${VALUE_KEY}${propertyKey}`];
},
set: function (value) {
this[`${VALUE_KEY}${propertyKey}`] = value != null && `${value}` !== 'false';
}
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment