Skip to content

Incompatible type for ES6 Proxy set handler using ES6 shorthand syntax #11445

Closed
@httpdigest

Description

@httpdigest

I recently tried to use the ES6 Proxy class to create immutable objects from given 'target' objects, using the following code:

function makeImmutable(target) {
  return new Proxy(target, {
    set(target, property, value, receiver) {
      throw '...';
    }
  });
}

Compiling this with:
tsc --lib es6 test.ts

fails with the following error message:

test.ts(2,28): error TS2345: Argument of type '{ set(target: any, property: PropertyKey, value: any, receiver: any): void; }' is not assignable to parameter of type 'ProxyHandler<any>'.
  Types of property 'set' are incompatible.
    Type '(target: any, property: PropertyKey, value: any, receiver: any) => void' is not assignable to type '(target: any, p: PropertyKey, value: any, receiver: any) => boolean'.
      Type 'void' is not assignable to type 'boolean'.

However, when not using the ES6 "function property" shorthand syntax, and instead doing the following:

function makeImmutable(target) {
  return new Proxy(target, {
    set: function(target, property, value, receiver) {
      throw '...';
    }
  });
}

Everything compiles fine.

Typescript version: 2.1.0-dev.20161007

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions