TypeScript-Handbook icon indicating copy to clipboard operation
TypeScript-Handbook copied to clipboard

Samples code in "Metadata" section of "Decorators" page is wrong

Open JeffreyZhao opened this issue 5 years ago • 0 comments

Currently (with comments added to explain the issue):

function validate<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) {
    let set = descriptor.set;
    descriptor.set = function (value: T) {
        let type = Reflect.getMetadata("design:type", target, propertyKey); // "target" should be "this"
        if (!(value instanceof type)) {
            throw new TypeError("Invalid type.");
        }
        set.call(target, value); // "target" should be "this"
    }
}

We should use this instead of target in those two lines with comments. I will make a PR for it later.

JeffreyZhao avatar Mar 02 '20 19:03 JeffreyZhao